Reputation: 535
i can get the following string
([{"data":{"Rate":"","RoleA":"Student","NameA":"student","RoleB":"Tutor","NameB":"tutorB","Give":"0","Get":"1","Accept":"0"}}]);
i have read from http://api.jquery.com/jQuery.ajax/ but still not sure where did i do wrong.
this is my code
$.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);
}
});
from chrome, i can find the json string, and it looks normal. however, it does not alert the success msg, instead it alert parsererror error..where should I change? Thanks
my 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 . ');';
?>
i am not sure why the URL is as follows Request URL: http://mydomain.com/check.php?callback=jsoncallback&nameB=tutorB&roleB=Tutor&get123=1&accept=0&_=1363710513593
the last parameter &_=1363710513593
i am not sure what is it?
but it can return the above string
does it related to jquery version? i used jquery-1.9.1.min.js
Upvotes: 0
Views: 2651
Reputation: 1700
You need to specify a callback name in the ajax settings object (right now its called "?"). Read more about how this works on http://api.jquery.com/jQuery.ajax/. Lookup the "jsonp" in the settings object. You will also need to get that callback name in your PHP code and return it in the response so that that function will execute when it is being received to the client. Example: jsoncallback(YOURJSONDATA).
Pasted from http://api.jquery.com/jQuery.ajax/
jsonp Type: String Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }
Upvotes: 3
Reputation: 17579
UPDATED: you are showing that your service is not returning proper jsonp payload
Fix it to use passed callback name, so you'll have the following response, when you passing in mycallback
as jsonp callback.
mycallback([{"data":{"Rate":"","RoleA":"Student","NameA":"student","RoleB":"Tutor","NameB":"tutorB","Give":"0","Get":"1","Accept":"0"}}]);
UPDATED 2: &_=1363710513593
- is timestamp added to request in order make jquery able to figure out what was corresponding jsonp request, because of the way how jsonp is handled on the client ( ie response comes as text and is embeded in the body )
json service you are accessing has support for jsonp , so you need to configure your $.ajax
to do jsonp request and change json callback name to jsoncallback
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',
success: function(data){
alert(data[0].data.RoleA);
}
},
error: function(){
alert('There was an error.');
}
});
//return false;
});
Upvotes: 1
Reputation: 19093
It looks like your server is trying to reutnr jsonp. Try adding
dataType: 'jsonp'
to your ajax request and drop the jsoncallback=? paramter
e.g.
$.ajax({
type: 'GET',
dataType: 'jsonp',
data:
{
//nameB: nameB,
//roleB: roleB,
//get123: get123,
//accept: accept
},
url: 'http://mydomain.com/check.php?nameB=tutorB&roleB=Tutor&get123=1&accept=0&',
success: function(data){
alert(data[0].data.RoleA);
},
error: function(){
alert('There was an error.');
}
});
//return false;
});
Upvotes: 1