Error: jQuery1830649454693285679_1359620502896 was not called JSON

I read many, many Q questions and the same problem, but I do not have my specific problem (at least I could not find this).

I have a php script that returns a json string

header('Content-Type: application/json'); echo $result; 

JSON returned (marked with JSONLint and valid):

 {"Announcement":{"ID":1,"Type":1,"Text":"This is a test Albums announcement.","TimeStart":"1969-12-31","TimeEnd":"1969-12-31"}} 

And a web jquery script that reads json:

 $.ajax({ type : "GET", url : "http://b***s.net/S****s/GetAnnouncements.php?callback=?", data : {get_param : "Announcement"}, dataType : "json", error : function(jqXHR, textStatus, errorThrown) {alert(errorThrown); alert(textStatus);}, success : function(data) {alert('success'); $.each(data, function(index, element) { alert('here'); $("#announcements-list").append("<li><a id='announcements-a-" + element.ID + "' href='#announcement-details'><p>" + element.Type + ": " + element.Text + "</p></a></li>"); $("#announcements-a-" + element.ID).bind('click', function() {Announcements.AnnouncementID = element.ID;}); }); $("#announcements-list").listview('refresh'); } }); 

success: never called. And error: returns a textStatus of "parsererror" and errorThrown is "Error: jQuery1830649454693285679_1359620502896 was not called"

  • I added callback=? in the url to get around the problem between domains.
  • I sent header('Content-Type: application/json'); in php and it returns NO html.
  • I checked the validity of JSON using JSONLint
  • I tried removing data: "json" as some answers say, but it still returns parsererror
  • Using jQuery 1.8.3
+4
source share
2 answers

Server and client scripts do not complement each other. You have two options:


Make the server side of the script return JSON:

 Content-Type: application/json {"Announcement":{"ID":1}} 

And omit the callback parameter:

 $.ajax({ type : "GET", url : "http://example.com/feed/json.php", dataType : "json" }); 

Make your server side script return JSONP ie JSON wrapped in a callback function:

 Content-Type: application/javascript jQuery_xxxxxxxx({"Announcement":{"ID":1}}); 

And change the data type to jsonp:

 $.ajax({ type : "GET", url : "http://example.com/feed/json.php", dataType : "jsonp" }); 

Note that jQuery silently adds &callback=jQuery_xxxxxxxx to the URL for such queries. The server must use the callback name specified in the URL. You can do something like this:

 echo sprintf( "%s(%s);", isset($_GET["callback"]) ? $_GET["callback"] : "void", json_encode($data) ); 
+4
source

I just tried, and here is the solution tested in the cross domain

 $.ajax({ type : "GET", url : "http://******/14621356.php", data : {get_param : "Announcement"}, dataType : "jsonp", error : function(jqXHR, textStatus, errorThrown) {alert(errorThrown); alert(textStatus);}, success : function(data) {alert('success'); $.each(data, function(index, element) { alert('here'); $("#announcements-list").append("<li><a id='announcements-a-" + element.ID + "' href='#announcement-details'><p>" + element.Type + ": " + element.Text + "</p></a></li>"); $("#announcements-a-" + element.ID).bind('click', function() {Announcements.AnnouncementID = element.ID;}); }); $("#announcements-list").listview('refresh'); } }); 

And for php

 header('Content-Type: application/json'); echo $_GET['callback'].'('.'{"Announcement":{"ID":1,"Type":1,"Text":"This is a test Albums announcement.","TimeStart":"1969-12-31","TimeEnd":"1969-12-31"}}'.')'; 

Take care of jsonp in dataType.

+2
source

All Articles