1

I have an ajax script that sends some data to an external URL. The external URL is hosted on the same server, however the domain is different than the source of the ajax call.

This is working perfectly in Firefox and Chrome. However in IE The ajax call does not go through, and the Return False function does not either work (once the ajax call fails).

Below is my code:

 $.get('http://myexternaldomian.com/feedback/save.php', {
            answer: $('#answer').val(),
            page_url: pathname
        });

        // Keeps the user on the page
        return false;

When I try removing the http:// from the ajax url, the return false does work.

Any help on this would be greatly appreciated. Thank You

7 Answers 7

2

From jQuery documentation

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

and Same Origin Policy on Wiki

Sign up to request clarification or add additional context in comments.

Comments

1

I'm surprised any of them are working. Browsers generally don't allow ajax calls to a domain other than the one the current page came from.

The main exception to this rule is if you make an ajax call using jsonp (json with padding). You can do this with jQuery, here's how. Look under the dataType option.

4 Comments

I thought it would not work based on some research i did. But it does work perfectly in FF and Chrome. I will look into your link. Thanks again
Please reread my answer, you may be following the first link which was incorrect. I edited it to be the correct link.
The jQuery docs also say the same. It says most 'AJAX' requests, I'm not sure if it means all. EDIT: got it, your answer has answer to my question!
If you dont mind, can you include an example of how my above code would look if I used that method? I am extremely new to Jquery, so don't really understand the application so well.
1

(this is copypaste from my another similar answer). You could try enabling "jQuery.support.cors=true" flag and see how it goes. I use jQuery v1.7.2.

I had to load webpage from local disk "file:///C:/test/htmlpage.html", call "http://localhost/getxml.php" url, and do this in IE8+ and Firefox12+ browsers, use jQuery v1.7.2 lib to minimize boilerplate code. After reading dozens of articles finally figured it out. Here is my summary.

  • server script (.php, .jsp, ...) must return http response header Access-Control-Allow-Origin: *
  • before using jQuery ajax set this flag in javascript: jQuery.support.cors = true;
  • you may set flag once or everytime before using jQuery ajax function
  • now I can read .xml document in IE and Firefox. Other browsers I did not test.
  • response document can be plain/text, xml, json or anything else

Here is an example jQuery ajax call with some debug sysouts.

jQuery.support.cors = true;
$.ajax({
    url: "http://localhost/getxml.php",
    data: { "id":"doc1", "rows":"100" },
    type: "GET",
    timeout: 30000,
    dataType: "text", // "xml", "json"
    success: function(data) {
        // show text reply as-is (debug)
        alert(data);

        // show xml field values (debug)
        //alert( $(data).find("title").text() );

        // loop JSON array (debug)
        //var str="";
        //$.each(data.items, function(i,item) {
        //  str += item.title + "\n";
        //});
        //alert(str);
    },
    error: function(jqXHR, textStatus, ex) {
        alert(textStatus + "," + ex + "," + jqXHR.responseText);
    }
});

Comments

0

http://en.wikipedia.org/wiki/Same_origin_policy

Comments

0

I dont think it should work on Chrome or Firefox, unless you testing on localhost or something like that, this would be against the crossdomain policy.

What you need is to proxy it inside the same domain, use php to connect to the destination you need and call the url from the same domain.

save_cross_domain.php -> connect through server to the desired url

then ajax calls save_cross_domain.php

Comments

0

you should add a

callback=?

to your url and handle this on the server side.

I did this once for a java servlet, and when the callback param was included I added an extra pair of parenteses around the json response..

hope it helps!

1 Comment

Interesting Idea. Thing is, I dont want to have to include any additional files/code on the source domain.
0

A couple of things:

  • The answers/conversation for this question has gone a bit out of context. Actually from the question it was more implied how to make ajax calls in IE. [Atleast modify the question title, else the question is very localized]

A couple of solutions to this cross-domain issue:

  1. CORS[compatible after IE7]
  2. JSONP [ here actually the browser takes in the input thinking it is a script]
  3. server side encoding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.