How does Jsonp work cross domain, i had a quick sample to see if i could solve a problem using code below access both a web api controler and a mvc controller return jsonresult. both returned the correct data in responsebody, but sucess function would not
fire.
And the endpoint you're hitting is definitely set up to do JSONP? I.e. returning JSON specifed as an arg to a function call?
What if you knocked up an HTML page with a <script> tag in it that hits your endpoint (with suitable "?callback=fnName" appended) - what is returned?
Do you need to specify "crossDomain: true" in your call, to allow the server to do a redirect to another domain?
If the server endpoint is set up to do JSONP, is it taking the function name from the callback= query string? It would be a mistake if it used a hardwired name (e.g. "success").
As for the controller being setuo to do jsonp, no i dont know. I have used a simple api get method returning string, and i have used a mvc action returning json, neither fire.
Andrew, jQuery handles wiring all that up for you automatically and as far as I can see he's wiring this up correctly.
http://api.jquery.com/jQuery.ajax/
My guess is the service isn't returning actual JSONP data?
Asfor the controller being setuo to
do jsonp,no i dont know. I have used a simple api
get method returning string,and i have used a mvc action returning json, neither fire.
The request URL looks right, but the return data is not, this is definnitely not JSONP :)
JSONP handlers looks for the callback=something parameter in the query string and uses that name to name the Javascript method that's created to wrap the JSON data.
JSONP is basically a hack to get data cross-domain by using the browser's ability to load Javascript over cross-domain boundaries.
JSONP isn't supported "out of the box" with ASP.NET Web API YET, but here's a sample to get you started:
http://stackoverflow.com/questions/9421312/jsonp-with-mvc-4-webapi
ThatsIT
Participant
1026 Points
388 Posts
Cross domain JsonP
Mar 14, 2012 12:43 PM|LINK
How does Jsonp work cross domain, i had a quick sample to see if i could solve a problem using code below access both a web api controler and a mvc controller return jsonresult. both returned the correct data in responsebody, but sucess function would not fire.
Any ideas?
Thanks
$(document).ready(function () { alert("") $.ajax({ url: 'http://localhost:5656/perth/api?callback=?', type: "GET", dataType: "jsonp", success: function (data) { alert(data) $('#divContent').append(data); } }); });awebb
Member
204 Points
91 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:09 PM|LINK
Doesn't jQuery add the "?callback=?" part for you?
ThatsIT
Participant
1026 Points
388 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:11 PM|LINK
I tried with and without still no luck
awebb
Member
204 Points
91 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:17 PM|LINK
And the endpoint you're hitting is definitely set up to do JSONP? I.e. returning JSON specifed as an arg to a function call?
What if you knocked up an HTML page with a <script> tag in it that hits your endpoint (with suitable "?callback=fnName" appended) - what is returned?
Do you need to specify "crossDomain: true" in your call, to allow the server to do a redirect to another domain?
If the server endpoint is set up to do JSONP, is it taking the function name from the callback= query string? It would be a mistake if it used a hardwired name (e.g. "success").
ThatsIT
Participant
1026 Points
388 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:41 PM|LINK
$(document).ready(function () { alert("") $.ajax({ url: 'http://localhost:2131/api/values/1', type: "GET", dataType: "jsonp", success: function (data) { alert(data) $('#divContent').append(data); } }); });"<iframe width=\"420\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/-Xu71i89xvs\" frameborder=\"0\" allowfullscreen><\/iframe>"
the same is returned using scriopt tag.As for the controller being setuo to do jsonp, no i dont know. I have used a simple api get method returning string, and i have used a mvc action returning json, neither fire.SiggiGG
Member
265 Points
105 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:46 PM|LINK
Andrew, jQuery handles wiring all that up for you automatically and as far as I can see he's wiring this up correctly.
http://api.jquery.com/jQuery.ajax/
My guess is the service isn't returning actual JSONP data?
SiggiGG
Member
265 Points
105 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:49 PM|LINK
The request URL looks right, but the return data is not, this is definnitely not JSONP :)
JSONP handlers looks for the callback=something parameter in the query string and uses that name to name the Javascript method that's created to wrap the JSON data.
JSONP is basically a hack to get data cross-domain by using the browser's ability to load Javascript over cross-domain boundaries.
JSONP isn't supported "out of the box" with ASP.NET Web API YET, but here's a sample to get you started:
http://stackoverflow.com/questions/9421312/jsonp-with-mvc-4-webapi
awebb
Member
204 Points
91 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:52 PM|LINK
I know that it's wired up automatically. His request (in the original post) is incorrect in that it shouldn't append ?callback=?
Totally.
ThatsIT
Participant
1026 Points
388 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:53 PM|LINK
this is what i get back using my mvc controller returning json
"\u003ciframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/-Xu71i89xvs\" frameborder=\"0\" allowfullscreen\u003e\u003c/iframe\u003e"
any ideas how I return jsonp, i have never used it before
SiggiGG
Member
265 Points
105 Posts
Re: Cross domain JsonP
Mar 14, 2012 01:53 PM|LINK
See my reply above, I just included a link to a solution.