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

levi
  • 23,693
  • 18
  • 59
  • 73

7 Answers7

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

Praveen Lobo
  • 6,956
  • 2
  • 28
  • 40
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);
    }
});
Whome
  • 10,181
  • 6
  • 53
  • 65
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.

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • 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 – levi May 23 '11 at 19:14
  • Please reread my answer, you may be following the first link which was incorrect. I edited it to be the correct link. – Brian Ball May 23 '11 at 19:15
  • 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! – Praveen Lobo May 23 '11 at 19:15
  • 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. – levi May 23 '11 at 19:20
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
neoeahit
  • 1,689
  • 2
  • 21
  • 35
0

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

Ryan Olds
  • 4,847
  • 28
  • 24
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

Bernardo Mendes
  • 1,220
  • 9
  • 15
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!

Runar Halse
  • 3,528
  • 10
  • 39
  • 59
  • Interesting Idea. Thing is, I dont want to have to include any additional files/code on the source domain. – levi May 23 '11 at 19:22