【发布时间】:2019-04-04 07:14:53
【问题描述】:
我已经设置了一个页面,该页面从表单中获取数据,序列化为 JSON,然后使用 AJAX 调用 PHP 文件来处理表单数据并通过 cURL 将其发送到 API。
如何从 API 获取响应以作为 AJAX 成功函数的一部分返回?
在我的项目开始时,我能够做到这一点,因为我使用 php 作为包含。但不能使用该方法,因为文件是从 AJAX 调用而不是从包含执行的。
我尝试关注this tutorial,但不断发现错误。
我还从这个网站上的各种帖子中搜索、审查和尝试了更多的建议,我什至无法计算。现在,我正在寻求帮助。
这是我的 index.php 文件中的相关 ajax。
$.ajax
({
type: "POST",
dataType : 'json',
async: false,
url: 'save_application.php',
data: { filename: fileName, applicationData: jsonFormString, job: adid },
success: function () { console.log("done");},
failure: function() {console.log('error');}
});
这里是 save_application.php 文件的相关部分。
$curl = curl_init();
curl_setopt_array($curl, array(
//stuff here
));
$applicantresponse = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
最后,返回的 $applicantresponse 格式如下:
{
"applicationId": 123456789,
"links": {
"link1": "https://thisisalinkforLINK1.html", //THIS IS THE VALUE I WANT
"link2": "https://thisisalink.html",
"link3": "https://thisisalink.html"
}
}
最终,我想将变量设置为 links->resume 的值(例如:var resumeLink = (something goes here); \\returns https://thisisalinkforLINK1.html) 回到我的成功函数中的 index.php 上,这样我就可以将该响应用于其他一些待办事项。
【问题讨论】:
-
您需要输出对 jQuery 代码的响应,这在此处不会发生。你的 PHP 有输出吗?