我可以得到以下字符串
([{"data":{"Rate":"","RoleA":"Student","NameA":"student","RoleB":"Tutor","NameB":"tutorB","Give":"0","Get":"1","Accept":"0"}}]);
我已经阅读了http://api.jquery.com/jQuery.ajax/但仍然不确定我在哪里做错了。
这是我的代码
$.ajax({
type: 'GET',
dataType: 'jsonp',
jsonpCallback: 'jsoncallback',
data:
{
nameB: nameB,
roleB: roleB,
get123: get123,
accept: accept
},
url: 'http://mydomain.com/check.php?callback=?',
success: function(data){
alert(data[0].data.RoleA);
//alert("ABC");
//if ( $("#role").text() == "Tutor" )
//{
// window.location.href='tutor_home.html';
//}
//else
//{
// window.location.href='student_home.html';
//}
},
error: function(jqXHR, textStatus){
alert("Request failed: " + textStatus);
}
});
从chrome,我可以找到json字符串,看起来很正常。但是,它不会提醒成功消息,而是提醒 parsererror 错误..我应该在哪里更改?谢谢
我的PHP
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/json");
include('mysqlConfig.php');
$nameB = $_GET["nameB"];
$roleB = $_GET["roleB"];
$get = $_GET["get123"];
$accept = $_GET["accept"];
$sql="SELECT * FROM tbl_rating WHERE NameB='$nameB' and RoleB='$roleB' and Get='$get' and Accept='$accept'";
$result=mysql_query($sql);
$rows = array();
//retrieve and print every record
while($r = mysql_fetch_assoc($result)){
// $rows[] = $r; has the same effect, without the superfluous data attribute
$rows[] = array('data' => $r);
}
// now all the rows have been fetched, it can be encoded
//echo json_encode($rows);
$data = json_encode($rows);
echo $_GET['jsoncallback'] . '(' . $data . ');';
?>
我不确定为什么 URL 如下请求 URL: http://mydomain.com/check.php?callback=jsoncallback&nameB=tutorB&roleB=Tutor&get123=1&accept=0&_= 1363710513593
最后一个参数&_=1363710513593
我不确定是什么?
但它可以返回上面的字符串
它与jquery版本有关吗?我用 jquery-1.9.1.min.js