Note that there are some explanatory texts on larger screens.

plurals
  1. PONot getting Response from ajax , getting into error function
    text
    copied!<p>I am working with ajax and node js for the first time. When I use the client to get response from node js it gets into the error function. On the other hand, when I directly call the url from the browser, I get the correct response from the server. This is an indication that something is wrong with the client side ajax request. </p> <p>This is my client code :</p> <pre><code>function fetch() { $.ajax({ type: 'POST', url: "http://127.0.0.1:8888", data: {data : 'India'}, dataType: 'json', success: function () { alert("hi"); //var ret = jQuery.parseJSON(data); //$('#q').html(ret.msg); }, error: function (jqXHR, textStatus, error) { console.log('Error: ' + error.message); alert("ERROR"); } }); </code></pre> <p>This is the server node.js code (part of it). The console.log msg get the correct values.</p> <pre><code>http.createServer(function(req, response) { console.log("Request received"); response.writeHeader(200, {"Content-Type": "application/json"}); req.on('data', function (chunk) { console.log(chunk.toString('utf8')); console.log(result[0].name); }); response.end(JSON.stringify({data:result})); }).listen(8888); </code></pre> <p>All the console.log in the server gets the correct values. That means the response is not reaching back when there is ajax request, but on directly writing 127.0.0.1:8888, I get correct response.</p> <p>The message on the console is :</p> <pre><code>XMLHttpRequest cannot load http://127.0.0.1:8888/. Origin null is not allowed by Access-Control-Allow-Origin. </code></pre> <p>Please someone help to fix this out. I have been on this for a day now.</p> <p>EDIT : Screen capture of the network tab. As you can see the server gets the request but the client does not get the response. This is done even after writing 'Access-Control-Allow-Origin' : '*'. <img src="https://i.stack.imgur.com/sDYAf.png" alt="enter image description here"></p>
 

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