Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery success function not firing using JSONP

Been doing some playing call my service which is on a different domain using jQuery. The call to the service is successfully made (my debug point gets tripped), and the correct response is returned (I sniff the traffic).

My problem is mainly that the success and failure callbacks don't get fired. I have read some other posts on SO that indicate the error event is not fired when using JSONP. Is that the case with the success event (perhaps because it is assumed that I am providing my own callback function), as well, or is there a way to fire my success callback. Thanks in advance.

$.ajax({
  type: "GET",
  url: urlOnDiffDomain,
  async: false,
  cache: false,
  dataType: 'jsonp',
  data: {},
  success: function(data, textStatus) {
    alert('success...');
  },
  error: function(xhr, ajaxOptions, thrownError) {
   alert('failed....');
  }
}); 
like image 563
Sanjay Uttam Avatar asked Mar 04 '10 15:03

Sanjay Uttam


2 Answers

Alright. In case anyone needs to know in the future...In hindsight, the solution probably should have been more obvious than it was, but you need to have the web-response write directly to the response stream. Simply returning a string of JSON doesn't do it, you need to someone construct it and stream it back. The code in my original post will work fine if you do indeed do that.

Example of service code:

public void DoWork() {   //it will work without this, but just to be safe   HttpContext.Current.Response.ContentType = "application/json";    string qs = HttpContext.Current.Request.QueryString["callback"];   HttpContext.Current.Response.Write(qs + "( [{ \"x\": 10, \"y\": 15}] )"); } 

Just for the sake of being explicit, this is the client-side code.

function localDemo(){   $.getJSON("http://someOtherDomain.com/Service1.svc/DoWork?callback=?",     function(data){       $.each(data, function(i,item){                     alert(item.x);       });   }); } 

If there is a better way to do this, I am all ears. For everyone else, I know there is some concept of native support in WCF 4.0 for JSONP. Also, you may want to do a little checking for security purposes - though I have not investigated much.

like image 62
Sanjay Uttam Avatar answered Sep 26 '22 20:09

Sanjay Uttam


The success callback method is called when the server responds. The $.ajax method sets up a function that handles the response by calling the success callback method.

The most likely reason that the success method is not called, is that the response from the server is not correct. The $.ajax method sends a value in the callback query string that the server should use as function name in the JSONP response. If the server is using a different name, the function that the $.ajax method has set up is never called.

If the server can not use the value in the callback query string to set the function name in the response, you can specify what function name the $.ajax method should expect from the server. Add the property jsonpCallback to the option object, and set the value to the name of the function that the server uses in the response.

If for example the $.ajax method is sending a request to the server using the URL http://service.mydomain.com/getdata?callback=jsonp12345, the server should respond with something looking like:

jsonp12345({...});

If the server ignores the callback query string, and instead responds with something like:

mycallback({...});

Then you will have to override the function name by adding a property to the options object:

$.ajax({
  url: urlOnDiffDomain,
  dataType: 'jsonp',
  data: {},
  success: function(data, textStatus) {
    alert('success...');
  },
  jsonpCallback: 'mycallback'
});
like image 45
Guffa Avatar answered Sep 24 '22 20:09

Guffa