2

I need to make an ajax post to a .NET web service on another domain. Can I set any return data coming from the server to jsonp and use js to read the data? I've come across this solution when posting to a .NET service:

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

$.ajax({
  type: "POST",
  url: "PageName.aspx/MethodName",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

I've used this before with much success, however my calls have always been within the same domain. Now that I would like to post cross-domain (is that even possible?), can the data that is returned be in jsonp to avoid any cross-domain issues.

Usually when I make GET requests cross-domain to let's say an API, I usually use "script" as my dataType in the ajax call and parse through the data with the callback function outside of the success ajax function.

Is it possible to use "script" as a dataType in the call above or how would I go about making this call and still treating the returned data as JSONP to be read inside of a callback function?

spez86
  • 732
  • 1
  • 11
  • 24

3 Answers3

0

When using jsonp you need to provide a callback name as a url querystring parameter. This is the name of the function that will be called on the client side when the data is returned. On the server side you need to do something like this:

if request.GET.has_key('callback'):
    callbackName=request.GET['callback']
    str="%s(%s)"%(callbackName,str)
return HttpResponse(str, mimetype='application/javascript')

For more info read http://api.jquery.com/jQuery.ajax/ and search for jsonp on the page.

p.s.: obviously my snippet is in Python but it's the same principle.

Sid
  • 7,511
  • 2
  • 28
  • 41
  • Thanks, that much makes sense. But I'm more curious about how to setup the actuall ajax call and what sort of dataType it should be declaring as it's calling. – spez86 Feb 06 '12 at 21:05
  • There's an example here: http://stackoverflow.com/questions/2067472/please-explain-jsonp Look at the answer with 8 Upvotes – Sid Feb 06 '12 at 21:08
  • @spez86 Here's the link to the example I mentioned in the above post. http://stackoverflow.com/a/6879319/559095 – Sid Feb 06 '12 at 21:18
  • @spez86 If this answer worked for you do you mind checking as correct :) – Sid Feb 06 '12 at 21:37
0

the data format of the return data is decided by the API on the server side. Some API's provide jsonp format. Look for the API documentation. On the client side making the call (unless you own the API), you do not have access for it.

0

JSONP isn't AJAX.

AJAX uses the browser's XHR interface to make a separate HTTP request under the limitation that the target of the XHR is same-origin as the page initiating the XHR.

JSONP on the other hand is a hack that became a standard. JSONP is the equivalent of... <script src="http://api.yoursite.com/whatever/call.json?callback=asdf

Where the file returned is really just a javascript that looks like... asdf({ ... json data ... });

That's all to get to this very important point:

You cannot do a cross-domain AJAX POST nor can you do a POST with JSONP. Why? JSONP is GET only since it's based on a <script> tag.

There are a handful of circumvention techniques for this, the easiest is probably the local proxy method where you setup a page on your server that makes the API request to the foreign domain server-side, then returns the results "locally".

T. Stone
  • 19,209
  • 15
  • 69
  • 97