代码之家  ›  专栏  ›  技术社区  ›  Geoff_S

通过ajax返回存储过程输出

  •  0
  • Geoff_S  · 技术社区  · 6 年前

    我目前有一个工作流程,在我的blade中进行ajax调用,它通过控制器进行调用,该函数使用PDO命中存储过程调用。此调用成功,存储过程正确执行/插入,并设置为返回输出。我现在唯一的问题是:

    $.ajax({
    
       type:'POST',
       url:'campaigns/createCampaign',
       data:{campaignName:campaignName, attribute:attribute},
        _token: '{{ csrf_token() }}',
       success:function(data){
            intro_modal.hide();
       }
    });
    

    public function createCampaign(Request $request)
    {
        $campaignName = $request->campaignName;
        $attribute = $request->attribute;
    
        $campaignService = new CampaignService();
        $createCampaign = $campaignService->createCampaign($campaignName, (int) $attribute);
    
        //return response()->$campaignService;
    }
    

    function createCampaign($campaignName, $attribute){
    
        $stmt = \DB::connection('odbc')->getPdo()->prepare('CALL PROCEDURES.INSERT_CAMPAIGN(?,?,?)');
    
        $stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
        $stmt->bindValue(2,$attribute, $attribute==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
        $stmt->bindParam(3,$out2, PDO::PARAM_INT);
    
        $stmt->execute();
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   loic.lopez    6 年前

    创建活动

    function createCampaign($campaignName, $attribute){
    
        $stmt = \DB::connection('odbc')->getPdo()->prepare('CALL PROCEDURES.INSERT_CAMPAIGN(?,?,?)');
    
        $stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
        $stmt->bindValue(2,$attribute, $attribute==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
        $stmt->bindParam(3,$out2, PDO::PARAM_INT);
        $stmt->execute();
    
        return $out2;
    }
    

    在你的

    使用这些类:

    use Illuminate\Support\Facades\Response;
    use Illuminate\Http\Response as HttpResponse;
    

    返回JSON响应:

    public function createCampaign(Request $request)
    {
        $campaignName = $request->campaignName;
        $attribute = $request->attribute;
    
        $campaignService = new CampaignService();
        $createdCampaignId = $campaignService->createCampaign($campaignName, (int) $attribute);
    
        return Response::json(["campaign_id" => $createdCampaignId)
                ->setStatusCode(HttpResponse::HTTP_OK);
    }
    

    在你的

    $.ajax({
    
       type:'POST',
       url:'campaigns/createCampaign',
       data:{campaignName:campaignName, attribute:attribute},
        _token: '{{ csrf_token() }}',
       success:function(data){
            intro_modal.hide();
           // data.campaign_id will contains the new campain id
       }
    });
    

    之后只需插入 data.campaign_id 具有 jQuery