Home > Web Front-end > JS Tutorial > How to handle ajax cross-domain access error 501

How to handle ajax cross-domain access error 501

php中世界最好的语言
Release: 2018-04-02 17:49:49
Original
2556 people have browsed it

This time I will show you how to deal with the ajax cross-domain access error 501, and what are the precautions for handling the ajax cross-domain access error. The following is a practical case, let's take a look.

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 when the contentType is application/x-www-form-urlencoded, multipart/form-data or text/plain, the browser will be triggered to send a request with the OPTIONS method first.

2 For example, your original request was a 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 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Ajax to add OA accounts based on human resources system data

How to make Ajax Keyword intelligent matching search

The above is the detailed content of How to handle 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