I have a students.json file on the remote server with this data
{
"studentId":101,
"firstName":"Carissa",
"lastName":"Page",
"emailId":"[email protected]"
}
Now i am trying to read students.json from different server (cross-domain) using jQuery.ajax().
This is my html page
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Metadata Test Page</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div id="studentdisplay">
<p>Getting Student Details</p>
</div>
</body>
</html>
I have this code in my JavaScript file
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://rupeshreddy.com/students.json',
contentType: "application/json",
dataType:"jsonp",
success: function (data) {
var output;
output = '<table><tr><th colspan=2>Student</th></tr>';
output += '<tr><td>First Name</td><td>'+ data.firstName +'</td></tr>';
output += '<tr class="alt"><td>Last Name</td><td>'+ data.lastName +'</td></tr>';
output += '<tr><td>Student Id</td><td>'+ data.studentId +'</td></tr></table>';
$("#studentdisplay").html( output );
}
})
.error(function(jqXHR, textStatus, errorThrown){
$("#studentdisplay").html(textStatus+" - "+errorThrown);
});
});
When i open the web page i getting this error:
parsererror - Error: jQuery111006872769086621702_1395648763612 was not called".
This same code works fine when both .html and .json file are on the same server (of course datatype will be then json). The error occurs only when both files are in different server.
I looked through so many past questions and articles, but non of them helped me solve this issue. Any help and suggestions are appreciated.
Link to JSFIDDLE http://jsfiddle.net/rL5fK/1/
===================================================
Update - Solved
I wrapped my data in students.json like this callback({...data...}) now my student.json was like this
callback({
"studentId":101,
"firstName":"Carissa",
"lastName":"Page",
"emailId":"[email protected]"
})
In my ajax call i added addition line jsonpCallback: 'callback'. Now my call was like this.
$.ajax({
url: 'http://rupeshreddy.com/students.json',
dataType: "jsonp",
jsonpCallback: 'callback',
success: function(data) {
console.log(data);
var output;
output = '<table border="1"><tr><th colspan=2>Student</th></tr>';
output += '<tr><td>First Name</td><td>'+ data.firstName +'</td></tr>';
output += '<tr class="alt"><td>Last Name</td><td>'+ data.lastName +'</td></tr>';
output += '<tr><td>Student Id</td><td>'+ data.studentId +'</td></tr></table>';
$("#studentdisplay").html( output );
}
});
Working JSFIDDLE link http://jsfiddle.net/esWSH/2/
Thanks everyone

