Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery ajax GET request to URL fails although HTTP status is 200 OK
    primarykey
    data
    text
    <p>I apologize if this question has already been answered.</p> <p>I am trying to retrieve data from a REST web service that exposes a JSON interface using jQuery .ajax call.</p> <p>When I call the service using the URL, the jQuery call fails although I get a HTTP status code 200 OK. </p> <p>When I copy the response into a file on the filesystem and retrieve this, the same call works.</p> <p>Both the file I am accessing and the web service I am calling are on the same machine. </p> <p>Some notes on the url used in the code below:</p> <p>Using:</p> <pre><code>url: "http://localhost:9090/app/user/861", </code></pre> <p>the call fails, goes into .fail on all browsers. </p> <p>The URL itself returns the json on all browsers: </p> <pre><code>{ "userid": 861, "employeeno": "123", "jobdesc": "Developer", "firstname": "Jasper", "lastname": "Fitussi" } </code></pre> <p>when using "test.json" in the local filesystem following is the behavior:</p> <pre><code>url: "ajax/test.json", </code></pre> <p>On Firefox, the call executes, goes into .done and displays the result on page.</p> <p>On Chrome, the call fails with status 404 and the following message - </p> <p>"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."</p> <p>I tried different combinations changing <code>dataType:"jsonp"</code>, adding a <code>?callback=?</code> to the end of the URL, and enclosing the data in the test.json with a '(' and a ')' without luck.</p> <p>Please understand I am new to UI programming, javascript and jQuery.</p> <p>Please help with what I am doing wrong. Here's the javascript:</p> <pre><code>&lt;script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $.ajax({ type: "GET", url:"ajax/test.json", // the following commented call fails, goes into .fail // url:"http://localhost:9090/app/user/861", contentType: "application/json", accepts: "application/json", dataType: "json" }) .done(function(data) { alert("Success"); console.log(data); var items = []; $.each( data, function( key, val ) { items.push( "&lt;li id='" + key + "'&gt;" + val + "&lt;/li&gt;"); }); $( "&lt;ul/&gt;", { "class": "my-new-list", html: items.join( "" ) }).appendTo( "body" ); }) .fail(function(data) { console.log(data); alert("Failed"); }) .always(function() { alert("In Always"); }); }); &lt;/script&gt; </code></pre> <p>The following is the output when I paste the url into the browser (also the contents of ajax/test.json):</p> <pre><code>{ "userid": 861, "employeeno": "123", "jobdesc": "Developer", "firstname": "Jasper", "lastname": "Fitussi" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload