0

I am making a cross-origin request using the jQuery 1.8.3 Ajax function. The code below always returns "fail" but "200 OK" is logged for each Ajax request in the console log. Given that the log shows a status of 200, shouldn't the success function be performed? How can I get the success function to be performed when a status of 200 is returned?

$().ready(function() {
    var checkIt = function(checkURL, resultP) {
    $.ajax({
      url: checkURL,
      type: 'GET',
      cache: false,
      success: function() {
        $(resultP).text("ok");
      },
      error: function() {
        $(resultP).text("fail");
      }
    });
  };
  checkIt("https://firstURL", "#p1p");
  checkIt("https://secondURL", "#p2p");
  checkIt("https://thirdURL", "#p3p");
)};

The console log shows the following (Each request has a 200 response)

[10:03:37.898] GET https://firstURL [HTTP/1.1 200 OK 141ms]
[10:03:37.898] GET https://secondURL [HTTP/1.1 200 OK 141ms]
[10:03:37.898] GET https://thirdURL [HTTP/1.1 200 OK 141ms]

Thanks!

  • 1
    possible duplicate of [Ajax with JQuery: 200 ok, but not "success"](http://stackoverflow.com/questions/4840825/ajax-with-jquery-200-ok-but-not-success) – JJJ Jan 07 '13 at 16:47
  • the answers to that question suggest using Google Books API or jsonp, neither of which work for me – Greg Helton Jan 07 '13 at 17:17
  • If the urls are not on the same domain then you're out of luck. – JJJ Jan 07 '13 at 17:18

2 Answers2

0

it could be possible that URL you are trying to GET data from was hit hence 200 but error or problem with the server you are trying to hit doesn't allow cross domain requests hence "fail".

Rahul
  • 928
  • 5
  • 8
0

This is not working since it is a cross domain call.

Work around for this

JavaScript

Create a function

function getMyData(data) {
    alert(data);
    //Do the magic with your data
}

Server side

On server end wrap your data inside function syntax

getMyData("Enter your data here");

JavaScript

Then create a script tag and add a link to your cross-domain page

 <script type="text/javascript"
         src="cross ref url">
 </script>

For reference: wikipedia

EDIT: Another option is Create a proxy on your domain. ie create a page in your domain which internally calls the cross-domain page and return the same data to your Ajax call.

Wolf
  • 2,150
  • 1
  • 15
  • 11
  • Thanks. I would like to do that but for the purposes of what I am doing, changing the server side is not an option. – Greg Helton Jan 07 '13 at 19:07