【发布时间】:2016-07-08 07:03:23
【问题描述】:
我正在尝试发送 JSON 对象,但结果它发送了一个解析错误。
{"readyState":4,"responseText":"","status":200,"statusText":"OK"}
这里是代码
var data = {
fb_id: response.id,
email: response.email,
name: response.name,
first_name: response.first_name,
last_name: response.last_name,
verified: response.verified,
birthday:response.birthday,
picture: response.picture.data.url,
hometown: response.hometown.name,
gender: response.gender
};
$.ajax({
url:'connect.php',
type: 'POST',
data: {
user: data
},
dataType: 'JSON',
success: function(html){
//page();
console.log(JSON.stringify(data));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.readyState == 4) {
console.log(JSON.stringify(XMLHttpRequest));
}
}
});
后端在这里: JSON 对象在这里发送
if(isset($_POST['user'])) {
//convert json object to php associative array
$data = $_POST['user'];
$fbid = $data['fb_id']; // To Get Facebook ID
$fbfullname = $data['name']; // To Get Facebook full name
$femail = $data['email']; // To Get Facebook email ID
$fbname = $data['first_name'];
$fblname = $data['last_name'];
$fbgender = $data['gender'];
$fverified = $data['verified'];
$faddress = $data['hometown'];
$fbirth = $data['birthday'];
$img = $data['picture'];
}
对象正在发送类似的东西:
{
"fb_id":"xxxxxxxxxxx99",
"email":"sxxxxxx@xxx.in",
"name":"Sagar xxxx",
...
}
PS:我使用的是1.12.4版本的jquery.min.js
更新 当我尝试使用此查询向 connect.php 页面发送请求时,它会在控制台日志中返回错误。如果我将 dataType 更改为“text”或将其排除,则它不会返回任何错误,但 connect.php 无法识别使用 ajax 请求发布的任何查询,即 isset($_POST['user']) 将无法识别任何查询。
【问题讨论】:
-
再写一些,到底是php还是js哪边出错了?