Home >Backend Development >PHP Tutorial >Detailed examples of solutions to the Ajax cross-domain access error 501
This article mainly introduces in detail the solution to the ajax cross-domain access error 501. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Problem: ajax cross-domain access error 501
Running the following code will report error 501
$.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'
Reason:
1 When cross-domain, except the contentType is application/x-www-form-urlencoded, multipart/form-data or text/plain In addition, the browser will be triggered to send a request with the OPTIONS method first.
2 For example, your original request is POST method. If the Allow attribute in the result header returned by the first request does not have a POST method,
3 then the second request is It will not be sent. At this time, the browser console will report an error, telling you that the POST method is not supported by the server.
Related recommendations:
Ajax cross-domain perfect solution example sharing
Ajax cross-domain request parsererror error solution
JS implements Ajax cross-domain request flask response content
The above is the detailed content of Detailed examples of solutions to the Ajax cross-domain access error 501. For more information, please follow other related articles on the PHP Chinese website!