开发者

Can a site block an ajax call referring to another, third party site?

So I have a bookmarklett that seems to not work on some sites.

Here's what happens. The bookmarklett (exists on your bookmark bar) fires a javascript sequence that will construct a div with content on any page you are currently on. Once the div is constructed, it will send an ajax GET request to a php page on my server (that is, the page the bookmarlett is loaded on is not within my server, this is somewhere else, ajax requests data from my server to be sent to the website the bookmarklett is loaded into).

Apparently, I have not seen this error myself, I was told that the ajax call failed to be received, so no dynamic data was passed into my constructed div.

The order of events is as followed: Bookmark Clicked -> Creates a modal popup div on the page being viewed -> runs an ajax call to my server, passing a unique ID of the user to retrieve some data -> then populates the dynamic content with this retrieved data.

Apparently on some site the ajax request was never received back.

Can some sites block my bookmarklett from gathering data via ajax from my site? Is it possible that the site messed up the request being sent to my server for a response?

If s开发者_开发知识库o, is there any work around? Or do i need to ditch it all together.

This is how my ajax request works, and I have not seen it fail, but apparently it has.. It may have been an error on the users part (may have touched some of the code supplying the data to send with the GET request, I'm not sure though)

jquery.ajax({
    type:"GET",
    url:"mysite.com/api/getStuff",
    data:"format=jsonp&userid=1234",
    success:function(data){
         // do stuff with the retrieved data
    },
    error:function(err, msg){
         // response was not received
    }
});


XMLHTTPRequest is subject to the same origin policy which is imposed by most browsers. For security reasons, it restricts XHR to the same protocol, domain, and port as the original page request.

Visiting the link I provided will also start you on your way to finding workarounds for this if it's something you want to do.


Crossdomain ajax requests are restricted. Use 'jsonp' or 'script' dataType.

Note that "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation". More info is available on official jquery documentation


I know from experience that some mod_rewrite rulz messed up my ajax requests, i think you might have some problems with using ajax + mod_rewrite.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜