【问题标题】:Returning stored procedure output through ajax通过ajax返回存储过程输出
【发布时间】:2019-09-08 03:48:29
【问题描述】:

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

如何获取存储过程的输出并将其传递回刀片以获取隐藏输入?没有页面刷新,所以当 AJAX 调用成功时,我想将服务文件的输出放入刀片中的隐藏输入中。我怎样才能正确地做到这一点?

刀片:

$.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();

}

【问题讨论】:

  • 我明白你想在隐藏输入中拥有最新的Campaign
  • 是的。在服务中,$out2 是我最新的活动 ID。一旦ajax调用成功,我只想将它作为隐藏输入,所以我需要在此之后返回$out2
  • 我会在回答中向您提出我的解决方案。

标签: javascript ajax laravel stored-procedures pdo


【解决方案1】:

在你的createCampaign

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);
}

在您的 Blade 模板中

$.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 即可。

【讨论】:

  • 谢谢!我确实认为这里有语法错误return Response::json(["campaign_id" => $createdCampaignId)
  • 我修复了这个问题,但现在console.log(data.campaign_id) 为空,所以我想看看为什么
  • 你能做到吗console.log(data)
  • 是的,它显示了对象OUT_CAMPAIGN_ID: null
  • 返回的 OUT_CAMPAIGN_ID 是一个整数,如果有区别的话
猜你喜欢
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
相关资源
最近更新 更多