Ajax request returns 200 OK, but an error event is fired instead of success

后端 未结 16 2112
难免孤独
难免孤独 2020-11-22 04:12

I have implemented an Ajax request on my website, and I am calling the endpoint from a webpage. It always returns 200 OK, but jQuery execut

相关标签:
16条回答
  • 2020-11-22 04:34

    Your script demands a return in JSON data type.

    Try this:

    private string test() {
      JavaScriptSerializer js = new JavaScriptSerializer();
     return js.Serialize("hello world");
    }
    
    0 讨论(0)
  • 2020-11-22 04:36

    Another thing that messed things up for me was using localhost instead of 127.0.0.1 or vice versa. Apparently, JavaScript can't handle requests from one to the other.

    0 讨论(0)
  • 2020-11-22 04:36

    I had the same problem. It was because my JSON response contains some special characters and the server file was not encoded with UTF-8, so the Ajax call considered that this was not a valid JSON response.

    0 讨论(0)
  • 2020-11-22 04:40

    This is just for the record since I bumped into this post when looking for a solution to my problem which was similar to the OP's.

    In my case my jQuery Ajax request was prevented from succeeding due to same-origin policy in Chrome. All was resolved when I modified my server (Node.js) to do:

    response.writeHead(200,
              {
                "Content-Type": "application/json",
                "Access-Control-Allow-Origin": "http://localhost:8080"
            });
    

    It literally cost me an hour of banging my head against the wall. I am feeling stupid...

    0 讨论(0)
提交回复
热议问题