Detailed examples of solutions to the Ajax cross-domain access error 501

小云云
Release: 2023-03-19 12:10:01
Original
1814 people have browsed it

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);
      }
    }
  });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!