Solution for ajax cross-origin access error 501: ajax501
Problem:Ajax cross-origin access error 501
If you run the following code, error 501 is returned.
$.ajax({ type: "POST", url: "http://192.168.1.202/sensordata.php", contentType:'application/json; charset=utf-8', data: JSON.stringify(ajaxPostData), dataType:'json', success: function(data){ //On ajax success do this console.info("success."); if (data["status"] == "ok"){ alert("Settings is Ok. The Machine is rebooting."); } }, error: function(xhr, ajaxOptions, thrownError) { //On error do this console.info("error."); if (xhr.status == 200) { alert(ajaxOptions); } else { alert(xhr.status); alert(thrownError); } } });
Solution:
Remove contentType: 'application/json; charset = UTF-8'
Cause:
1 In cross-origin, except for the contentType of application/x-www-form-urlencoded, multipart/form-data, or text/plain, will trigger the browser to first send a request with the method of OPTIONS.
2. For example, your original request is POST by method. If the Allow attribute in the result Header returned by the first request does not have the POST method,
3. The second request will not be sent, and an error will be reported in the browser console, telling you that the POST method is not supported by the server.
Reference: http://www.foreverpx.cn/2016/06/22/cross_content_type/
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.