gpt4 book ai didi

javascript - 通过ajax返回存储过程输出

转载 作者:行者123 更新时间:2023-11-30 19:37:58 24 4
gpt4 key购买 nike

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

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

Blade :

$.ajax({

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

Controller :

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

}

最佳答案

在您的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;
}

在你的 Controller

使用这些类:

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

之后,您只需使用 jQuery 插入 data.campaign_id 的值即可。

关于javascript - 通过ajax返回存储过程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55735314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com