Ajax请求失败,看不到原因 [英] Ajax request fails, can't see why

查看:852
本文介绍了Ajax请求失败,看不到原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个返回json中数据的API,现在我正在创建一个应用来通过接收数据并在页面上使用它来测试API.但是,我的ajax代码(下面的se)由于某种原因而失败,并且错误仅显示错误"(错误:错误).

I've created an API that returns data in json, and now I'm creating an app to test the API by receive the data and use it on the page. However, my ajax code (se below) fails by some reason, and the error says just "error" (Error: error).

代码有什么问题?

代码:

    $.ajaxSetup ({
      url: "http://localhost:8000/products", // <--- returns json
      type: "GET",
      headers:{
        "Accept":"application/json",
        "Content-type":"application/x-www-form-urlencoded"
      }
    })
    $.ajax({
        success: function(data){
            console.log("You made it!");
        },
        error: function(xhr) {
           console.log("Error: " + xhr.statusText);
       }
    }).done(function(data){
        console.log(data);
    })

这是我在Chrome(检查器->网络)中获得的信息:

This is the info I get in Chrome (Inspector -> Network):

名称:产品localhost/,方法: GET,状态:(已取消),类型:待定,启动器: jquery-latest.js:8434脚本,大小: 13B 0B,时间: 95ms 0.0天

Name: products localhost/, Method: GET, Status: (cancelled), Type: Pending, Initiator: jquery-latest.js:8434 Script, Size: 13B 0B, Time: 95ms 0.0 days

推荐答案

$.ajax({
    /* ajax options omitted */
    error: function (xmlHttpRequest, textStatus, errorThrown) {
         if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
              return;  // it's not really an error
         else
              // Do normal error handling
});

这篇关于Ajax请求失败,看不到原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆