开发者

IIS Configuration to allow cross domain JSONP requests from a JavaScript file

开发者 https://www.devze.com 2023-03-25 16:57 出处:网络
I have an HTML and JavaScript file set up to retreive data from a JSONP enabled web service. My problem is that when I deploy the html and .js file to the directory where the service is running it wil

I have an HTML and JavaScript file set up to retreive data from a JSONP enabled web service. My problem is that when I deploy the html and .js file to the directory where the service is running it will execute fine, but if I try to run the html and .js file from another host, the request doesn't make it to the web service and the java script gives me a server 500 error.

I'm using Chrome's developer tools to see the xmlhttprequest call which is the only reason I know I'm getting the 500 error.

I've already done quite a bit开发者_JAVA百科 of research and found that I need to have the script method tag set with the response type set to json and the usehttpget set to true, and I've done these two things as well as tried setting up a clientaccesspolicy.xml in the project as was mentioned by another post. All to no avail.

I'm sure this is a configuration issue on the web service or IIS side as when I put the html and javascript files in the directory of my code and run the web service through the IDE, I can use that instance of the html to call the web service and I get my response fine. But if the html and javascript isn't in the working directory of the code being ran, I get a server error 500.

How can this be solved?

EDIT I also found a post saying that the 500 error was based on the request size so I added < jsonSerialization maxJsonLength="5000000" / > to my web.config to no help.


I don't know much about IIS configuration, but if you are trying to use JSONP to get around a cross-domain issue, then you should not see an XMLHttpRequest. The way JSONP works is that you inject a script tag with the url you are trying to call:

var tag = document.createElement("script")
tag.src = "http://someurl.example.com?args=42&callback=wrapperFunction"
document.body.appendChild(tag)

The server then uses the callback querystring argument to wrap it's response in a functioncall, like this:

wrapperFunction({some: 'response', arg: 42});

The reason this works is that script-tags doesn't have to follow cross-domain policies, so if you aren't doing it this way, then you are not using JSONP and you have to obey the cross-domain rules.

I apologize if this really is a problem with the IIS configuration, I just saw XMLHttpRequest mentioned together with JSONP, and tought "hey that's wrong!". I hope it's helpful, else just disregard it :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消