发送POST请求&;在PHP中获取JSON响应?
我正试图通过PHP从AJAX发送一个发送POST请求&;在PHP中获取JSON响应?,php,ajax,curl,post,php-curl,Php,Ajax,Curl,Post,Php Curl,我正试图通过PHP从AJAX发送一个POST请求。我和邮递员一起检查了API。它工作得很好。但它并没有在PHP中执行。它也不会显示在“网络”选项卡中 我在Stack Overflow中看到了许多用于发出POST请求的示例,并尝试了它。但我不知道我错在哪里 我在这里附上了JS代码和PHP代码 JavaScript代码 function editUser(toid, name, mobile, mail, pin, addr, state, dis, subdis, role, user) {
POST请求。我和邮递员一起检查了API。它工作得很好。但它并没有在PHP中执行。它也不会显示在“网络”选项卡中
我在Stack Overflow中看到了许多用于发出POST请求的示例,并尝试了它。但我不知道我错在哪里
我在这里附上了JS代码和PHP代码
JavaScript代码
function editUser(toid, name, mobile, mail, pin, addr, state, dis, subdis, role, user) {
$.ajax({
type: "POST",
url: "edituser.php",
dataType: 'html',
data: {
id: toid,
fullname: name,
phone: mobile,
email: mail,
address1: addr,
state: state,
district: dis,
subdistrict: subdis,
pincode: pin,
usertype: user,
role: role,
token: apptoken,
},
success: function (response) {
visibility(false);
console.log("Response > > " + response);
if (response.status == "SUCCESS") {
swal("Updated User", " Information Updated Successfully!", "success");
}
loadData();
}
});
}
<?php
// where are we posting to?
$url = 'http://api.tech.com/api/UpdateUser';
// what post fields?
$fields = array(
'id' => $_POST['id'],
'fullname' => $_POST['fullname'],
'phone' => $_POST['phone'],
'email' => $_POST['email'],
'address1' => $_POST['address1'],
'state' => $_POST['state'],
'district' => $_POST['district'],
'subdistrict' => $_POST['subdistrict'],
'pincode' => $_POST['pincode'],
'usertype' => $_POST['usertype'],
'role' => $_POST['role'],
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
$token = $_POST['token'];
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("AppToken: $token",
"Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// execute post
$result = curl_exec($ch);
echo $result;
// close connection
curl_close($ch);
?>
PHP代码
function editUser(toid, name, mobile, mail, pin, addr, state, dis, subdis, role, user) {
$.ajax({
type: "POST",
url: "edituser.php",
dataType: 'html',
data: {
id: toid,
fullname: name,
phone: mobile,
email: mail,
address1: addr,
state: state,
district: dis,
subdistrict: subdis,
pincode: pin,
usertype: user,
role: role,
token: apptoken,
},
success: function (response) {
visibility(false);
console.log("Response > > " + response);
if (response.status == "SUCCESS") {
swal("Updated User", " Information Updated Successfully!", "success");
}
loadData();
}
});
}
<?php
// where are we posting to?
$url = 'http://api.tech.com/api/UpdateUser';
// what post fields?
$fields = array(
'id' => $_POST['id'],
'fullname' => $_POST['fullname'],
'phone' => $_POST['phone'],
'email' => $_POST['email'],
'address1' => $_POST['address1'],
'state' => $_POST['state'],
'district' => $_POST['district'],
'subdistrict' => $_POST['subdistrict'],
'pincode' => $_POST['pincode'],
'usertype' => $_POST['usertype'],
'role' => $_POST['role'],
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
$token = $_POST['token'];
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("AppToken: $token",
"Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// execute post
$result = curl_exec($ch);
echo $result;
// close connection
curl_close($ch);
?>
更新:
发送到API($url)的请求未显示在“网络”选项卡中。但是显示了对edituser.php的请求
我觉得这里缺少很多上下文,但我已经做了很多类似的事情,所以我会尽力帮助大家了解我所了解的内容
以下是我从你的问题中收集到的信息
您正在JavaScript中调用一个ajax函数,该函数触发给定的PHP代码
JavaScript成功地“根据网络截图”调用了此代码
网络选项卡表示PHP脚本不返回任何内容
以下是我对如何前进的想法
您似乎正在从客户端JavaScript发送所有数据,包括安全pin和令牌。这是一个非常糟糕的想法,因为这意味着你的用户都可以看到这个安全pin,并为自己的邪恶目的窃取它。而是在php代码中存储诸如安全pin之类的常量
如果您希望在网络选项卡中看到curl请求,那么您就错了。curl请求从一个服务器到另一个服务器,客户端永远不会看到它
如果response选项卡为空,则可能会出现语法错误或某种没有捕获的curl错误
在调试请求中添加以下内容:
$result = curl_exec($request);
$response_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
echo 'Response code: ' . $response_code;
if(curl_error($request))
{
echo '<br />Curl error: ' . curl_error($request);
}
$result=curl\u exec($request);
$response\u code=curl\u getinfo($request,CURLINFO\u HTTP\u code);
回显“响应代码:”$响应代码;
if(curl_错误($request))
{
echo'
Curl error:'.Curl_error($request);
}
您至少应该在ajax调用的“response”选项卡中看到响应代码
如果仍然看不到任何内容,请确保设置为显示所有警告和错误等,并且不抑制所需的信息。您不能回显数组回显json_decode($result);,你是说json_编码吗?网络选项卡根本不显示请求发出?控制台中是否有任何错误?是否尝试使用PHP接收数据?您的PHP代码并不表示您是。请澄清PHP代码实际上表明$\u POST
是一个全局变量,在收到帖子时填充。“它没有显示在网络选项卡中”,那么您的editUser()
函数可能没有被调用。api可能会有该响应代码的特定答案,但可以在此处找到泛型。“415:Unsupported Media”这可能意味着curl请求上的http头需要有一个“Content-Type:xxx”指令。