开发者

AJAX call works on local machine, not on host

开发者 https://www.devze.com 2022-12-28 01:56 出处:网络
What could potentially stop an AJAX call from working on the host server, when it works fine on the local host? I tried returning an error from the AJAX call, but all I get is \'undefined\'. I don\'t

What could potentially stop an AJAX call from working on the host server, when it works fine on the local host? I tried returning an error from the AJAX call, but all I get is 'undefined'. I don't think the actual page m开发者_JS百科ethod is being called since no information is added in my log (and I've explicitly added a call). I can't reproduce the problem on my local machine, so does anyone know possible areas I should look into?

Thanks


What URL is in your Browser, and What URL is your Ajax call hitting? Browsers have 'security' constraints that don't allow cross domain AJAX calls. So for instance, if I am loading a local file:

http://localhost:20080/myCustomPage.html

and within that page, I make an ajax call to

http://search.twitter.com/search?q=test

I would get a security warning from the browser. Depending on your browser this may manifest itself in a warning icon in the bottom corner (IE), or an error in the javascript log (firefox).

There is a way to get around this in ONE specific case. That case being any GET request. To do this instead of making an 'ajax' call you include a tag. That script tag will then be read and loaded. The catch here, is the call now needs to include a "callback" method, that you can implement on your side, that gets called with the result of the call.

So instead of a response like:

{
    "first_name": "peter",
    "last_name": "parker"
}

you would need to return

myCallBackFunction({
    "first_name": "peter",
    "last_name": "parker"
});

This example is using JSON, but you could easily use XML, HTML or any other result format as long as the function is called.

myCallbackFunction("INSERT RESPONSE TEXT HERE")

This method is commonly refereed to as JSONP and is fortunately implemented in the common javascript libraries like jquery from the client perspective. If you control the server side, you will need to hard code a callBackFunction wrapper, or expose a parameter that allows the client to set it. And unfortunately if you don't own the library there isn't much you can do unless the owner of the service already provides that feature. Fortunately most Web 2.0 services you would be doing stuff like this, already implement that feature.


I've managed to fix it. There was an issue with running IIS7 in integrated mode. I moved all my modules from to and my to (also in the section)

Hope that helps anyone else.

More info

0

精彩评论

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