0

I'm trying to send an ajax post request to a node.js microservice through nginx. The requuest is as follows:

$.post('http://localhost:80/api/signup', formData, function( data, status) {
    console.log(JSON.stringify(data));
    console.log(JSON.stringify(status));
        }).done(function(data, status) {
            alert( "second success" );
            console.log(JSON.stringify(data));
            console.log(JSON.stringify(status));
        })
        .fail(function(data, status) {
            console.log('error');
            console.log(JSON.stringify(data));
            console.log(JSON.stringify(status));
        })
        .always(function(data, status) {
            console.log(JSON.stringify(data));
            console.log(JSON.stringify(status));
            console.log('fiished');
        });

The request reaches the microservice. But the response is as follows (which is always the error function):

"data" is always:

{"readyState":0,"status":0,"statusText":"error"}

And the "status" is always:

error

which is not the expected response at success nor failure. How can I fix this?

Moreover, "formData" parameters appear as queries on the url once submitted. How can I stop that?

Update*

I have also tried adding event.preventDefault(); but now it gives me the following:

 XMLHttpRequest cannot load http://localhost:3000/api/signup. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
{"readyState":0,"status":0,"statusText":"error"}

I have also tried:

$.ajax({  
        type: "POST",  
        url: "http://localhost:3000/api/signup",  
        data: JSON.stringify(formData),  
        success: function(dataString) {  
            console.log(JSON.stringify(dataString));
        }, error: function(error) {
            console.log(JSON.stringify(error));
        }
    });

But got the same No 'Access-Control-Allow-Origin' header is present on the requested resource as above.

Update There is a valid and possibly more detailed answer on "No 'Access-Control-Allow-Origin' header is present on the requested resource". However, I find the answer I accepted here more direct and clear.

TamerB
  • 1,401
  • 1
  • 12
  • 27
  • Sounds like you're not preventing your form from submitting normally (ie `event.preventDefault()`) – Phil Jul 26 '17 at 04:24
  • Now it gives me `No 'Access-Control-Allow-Origin' header is present` error – TamerB Jul 26 '17 at 04:31
  • @Phil Please read the question update. – TamerB Jul 26 '17 at 04:44
  • Possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – Phil Jul 26 '17 at 05:05

1 Answers1

0

Are you executing the ajax request from the browser or localhost? If you are executing it from your browser it may be CORS issue. If you want to be able to execute that ajax request from your browser you should modify your server configuration/code to allow cross-origin HTTP requests.

This looks similar to your issue. CORS issue in Jquery Ajax Post

88jayto
  • 659
  • 1
  • 5
  • 20
  • I updated my backend and also added a contentType to the request and removed the JSON.stringify() of formData. Now it works. Thank you @88jayto – TamerB Jul 26 '17 at 05:09