[英]jQuery $.post not returning JSON data
I've read multiple similar posts on this, and my code seems to match the suggestions, but still no data returned.我已经阅读了多篇关于此的类似帖子,我的代码似乎符合建议,但仍然没有返回数据。
Here's my JS:这是我的 JS:
$.post('php/get_last_word.php', { user_id : userID },
function( data ) {
currentLanguage = data.language_id;
currentWord = data.word_id;
console.log("currentLanguage = " + currentLanguage)
console.log("currentWord = " + currentWord);
},'json');
And the relevant php:以及相关的php:
$user_id=$_POST['user_id'];
mysql_select_db(wordsicle_search);
$sql = "SELECT `language_id`, `word_id` FROM `save_state` WHERE `user_id`=$user_id";
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_assoc($result)) {
$encoded = json_encode($row);
echo $encoded;
}
And the resulting log:以及生成的日志:
Connected successfully{"language_id":"1","word_id":"1"}
So the json array is being echoed, but it's not ending up in data
, because currentLanguage
and currentWord
are not being populated.所以 json 数组被回显,但它并没有在
data
中结束,因为currentLanguage
和currentWord
没有被填充。 Is this a problem with asynchronicity?这是异步性的问题吗? Or something else?
或者是其他东西?
Make sure you have a valid json coming back to your variable from your PHP script确保你有一个有效的 json 从你的 PHP 脚本返回你的变量
IF your json object is like this,如果你的 json object 是这样的,
{"language_id":"1","word_id":"1"}
You can access the values like this您可以像这样访问值
currentLanguage = data.language_id;
currentWord = data.word_id;
Example JsFiddle http://jsfiddle.net/NuS7Z/8/示例 JsFiddle http://jsfiddle.net/NuS7Z/8/
You can use http://jsonlint.com/ to verify your jSon is in correct form or not.您可以使用http://jsonlint.com/来验证您的 jSon 的格式是否正确。
Specifying json
as the data type value in your post request will make sure the reponse is coming back as json
format to the success callback.在您的发布请求中指定
json
作为数据类型值将确保响应以json
格式返回到成功回调。
$.post('php/get_last_word.php',{user_id:userID}, dataType:"json",function(data){
currentLanguage = data.language_id;
currentWord = data.word_id;
});
You can also use getJson
to simply get json data.您也可以使用
getJson
来简单地获取 json 数据。 getJson is a shorthand of ajax Call with datatype as json getJson 是 ajax 的简写 Call with datatype as json
http://api.jquery.com/jQuery.getJSON/ http://api.jquery.com/jQuery.getJSON/
Try changing your JS to:尝试将您的 JS 更改为:
$.getJSON('php/get_last_word.php', { user_id : userID },
function( response ) {
if (response.success) {
currentLanguage = response.data.language_id;
currentWord = response.data.word_id;
console.log("currentLanguage = " + currentLanguage)
console.log("currentWord = " + currentWord);
} else {
alert('Fail');
}
});
and your PHP to:和您的 PHP 至:
<?php
$success = false;
$data = null;
$user_id=$_POST['user_id'];
mysql_select_db(wordsicle_search);
$sql = "SELECT `language_id`, `word_id` FROM `save_state` WHERE `user_id`=$user_id";
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_assoc($result)) {
$success = true;
$data = $row;
}
// I generally format my JSON output array like so:
// Response
header('Content-Type: application/json');
echo json_encode(array(
'success' => $success,
'data' => $data
));
?>
That way its more organized and don't forget to set the content type.这样更有条理,不要忘记设置内容类型。
Hope that helps.希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.