title: Ajax POST to Node.js through Nginx not getting correct response
body: <p>I'm trying to send an ajax post request to a node.js microservice through nginx. The requuest is as follows:</p>
  
  <pre><code>$.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');
          });
  </code></pre>
  
  <p>The request reaches the microservice. But the response is as follows (which is always the error function):</p>
  
  <p>"data" is always:</p>
  
  <pre><code>{"readyState":0,"status":0,"statusText":"error"}
  </code></pre>
  
  <p>And the "status" is always:</p>
  
  <pre><code>error
  </code></pre>
  
  <p>which is not the expected response at success nor failure.
  How can I fix this?</p>
  
  <p>Moreover, "formData" parameters appear as queries on the url once submitted. How can I stop that?</p>
  
  <p><strong>Update*</strong></p>
  
  <p>I have also tried adding <code>event.preventDefault();</code> but now it gives me the following:</p>
  
  <pre><code> 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"}
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>$.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));
          }
      });
  </code></pre>
  
  <p>But got the same <code>No 'Access-Control-Allow-Origin' header is present on the requested resource</code> as above.</p>
  
  <p><strong><em>Update</em></strong>
  There is a valid and possibly more detailed answer on <a href="https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource">&quot;No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource&quot;</a>. However, I find the answer I accepted here more direct and clear.</p>