0

So i'm trying to make a simple ajax call to retrieve some data from a file on a server. I can access the file through the web browser, and i've followed tutorials on this subject to the T, but it has gotten me nowhere. Here is the javascript code:

<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
  type: "POST",
  url: "https://www.mychoicetechnologies.com/Services/FMSUtilities.asmx/GetServerDate",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (msg) 
  {        
     document.getElementById('area').innerHTML = "Success! Retrieved a server response using AJAX.";
  },
  error: function (xhr, status, error)
  {        
     document.getElementById('area').innerHTML =  "1." + error +   "<br>";
     document.getElementById('area').innerHTML += "2." + xhr +     "<br>";
     document.getElementById('area').innerHTML += "3." + status +  "<br>";
     document.getElementById('area').innerHTML += "The script has failed";
  }
});
</script>

When I run this script the output is:

1.

2.[object Object]

3.error

The script has failed.

Sean Raia
  • 23
  • 4

1 Answers1

0

When I run this in Chrome, the console says:

XMLHttpRequest cannot load https://www.mychoicetechnologies.com/Services/FMSUtilities.asmx/GetServerDate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. (index):1

This means the service you are trying to hit does not allow requests from other domains.

See XMLHttpRequest cannot load, No 'Access-Control-Allow-Origin' header is present on the requested resource.

Edited to add:

It would make more sense to do a GET instead of a POST, since you are only trying to read, not write. And the service is returning XML, not JSON.

Community
  • 1
  • 1
Alex Wittig
  • 2,800
  • 1
  • 33
  • 42
  • Thanks, I'm now looking into using Access-Control-Allow-Origin on the server-side to allow my script through. If I elminate dataType: "json" this becomes CORS protocol, correct? – Sean Raia Mar 05 '14 at 20:41
  • I've not used CORS much, but my impression is that it's only dependent on the Origin/Access-Control-Allow-Origin headers. `dataType` simply affects how JQuery handles the response once it comes back. – Alex Wittig Mar 05 '14 at 20:55
  • Thanks a ton, I've got this sorted out properly. My next issue is one of retrieving the page's content. http://stackoverflow.com/questions/22227139/xml-retrieval-with-ajax-returning-document-object-how-do-i-get-the-content – Sean Raia Mar 06 '14 at 14:20