0

js server on an AWS and im trying to do a register page. i get the data from a form, and then by ajax send it to the server which stores the user information in a MongoDB, the ajax goes to the server and the user is stored however i dont get a response from the server. it should send me a json with the message, user already in database.

here i have the ajax request

$.ajax({
    type: 'POST',
    url: 'http:xxxxxxxxx/api/register',
    dataType: "json",            
    data: data,
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert('hola');
    }
});

it also says that the request method type is 'OPTION'

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Puzzero
  • 63
  • 1
  • 10
  • You don't appear to be using the `data` variable anywhere? Standard AJAX debugging rules apply. Check the console for errors. It sounds like may be being blocked by the Same Origin Policy. You may need to add CORS headers to the response of the server if so - assuming you have access to it. What makes you think you don't get data back? – Rory McCrossan Nov 02 '16 at 19:49
  • im using the data variable i set it before the ajax. I dont have console errors. The ajax is working correctly however it is using request method OPTION beacuse in the server the request comes as option – Puzzero Nov 02 '16 at 20:19
  • 1
    I mean the `data` variable in the `success` handler function. The OPTION request is the pre-flight which is required for cross-domain requests - which again would point to a SOP/CORS problem. Are you 100% sure there's no errors in the console? Did you check the response code of the request in the 'requests' tab? – Rory McCrossan Nov 02 '16 at 20:27
  • i checked, there are not errors on the console. Niether in the firefox browser or in the server – Puzzero Nov 02 '16 at 20:34
  • I found a solution to the problem. Dont know how, and why it works. i just Stringify the data to send – Puzzero Nov 03 '16 at 18:11

1 Answers1

0

The reason for the error is the origin policy. It only allows you to do XMLHTTPRequests to your own domain. See if it works:

$.getJSON( 'http://xxxxxxxxx/api/<some_resource>', function ( data ) { alert ( data ); }

If it's returning data so the problem is with the request itself. But probably you won't receive any response because of origin policy.

  • I think that the problem is with the sending ajax, it keeps changing the request method to OPTION, i have the same json on a python request program and it is working correctly – Puzzero Nov 02 '16 at 20:21
  • Did you try change it to use `jsonp` instead? – andrehigher Nov 03 '16 at 11:50
  • No i didnt try, i never used jsonp. Does the json response from the server should be a jsonp aswell? – Puzzero Nov 03 '16 at 13:02
  • Nope, you can keep the same response from the server. Using `jsonp` you'll be able to create a callback function to get the data from your server. Take a look at it: http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp – andrehigher Nov 03 '16 at 13:27
  • I found a solution to the problem. Dont know how, and why it works. i just Stringify the data to send – Puzzero Nov 03 '16 at 18:11