Jquery ajax调用获取';错误';使用swagger rest api

Jquery ajax调用获取';错误';使用swagger rest api,jquery,ajax,curl,Jquery,Ajax,Curl,我使用了一个招摇过市的脚本来生成RESTAPI { "swagger": "2.0", ... }, "host": "localhost:8080", "basePath": "/swagger-item-jaxrs", "schemes": [ "http" ], "consumes": [ "application/json" ], "produces": [ "application/json" ], "paths": { "/item/{cd}": { "get": {

我使用了一个招摇过市的脚本来生成RESTAPI

{
  "swagger": "2.0",
  ...
},
"host": "localhost:8080",
"basePath": "/swagger-item-jaxrs",
"schemes": [ "http" ],
"consumes": [ "application/json" ],
"produces": [ "application/json" ],
"paths": {
"/item/{cd}": {
  "get": {
    "description": "Returns an item based on the item code passed.",
    "operationId": "findItemById",
    "produces": [
      "application/json",
      "application/xml",
      "text/xml",
      "text/html"
    ],
    "parameters": [
      {
        "name": "cd",
        "in": "path",
        "description": "CD of item to fetch",
        "required": true,
        "type": "string"
      }
    ],
    "responses": {
      "200": {
        "description": "item response",
        "schema": {
          "$ref": "#/definitions/item"
        }
      },
      "default": {
        "description": "unexpected error",
        "schema": {
          "$ref": "#/definitions/errorModel"
        }
      }
    }
  }
}
},
"definitions": {
"item": {
  "type": "object",
  "required": [
    "cd",
    "name"
  ],
  "properties": {
    "cd": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
},
"errorModel": {
  "type": "object",
  "required": [
    "code",
    "message"
  ],
  "properties": {
    "code": {
      "type": "integer",
      "format": "int32"
    },
    "message": {
      "type": "string"
    }
  }
}
}
}
我可以使用客户端调用api生成java类,甚至可以将url输入浏览器并获得响应

浏览器:

答复:
{“cd”:“0446840”,“name”:“ROYAL BASMATI RICE”}

Swagger UI甚至可以返回相同的响应,并指示curl命令为:
curl-xget“http://localhost:8080/swagger-项目jaxrs/item/0446840“-H”接受:应用程序/json“

我的ajax代码:

$.ajax({
url:“http://localhost:8080/swagger-项目jaxrs/item/0446840”
,方法:“获取”
,标题:{“接受”:“应用程序/json”}
})
.完成(功能(数据){
警惕(“成功”);
console.log('success:');
控制台日志(数据);
})
.失败(功能(错误){
console.log('error:');
控制台日志(err);
});

Chrome开发者工具控制台结果:

错误:
对象{readyState:0,getResponseHeader:function,getAllResponseHeaders:function,setRequestHeader:function,OverrideMetype:function…}


非常感谢您的帮助。

我发现您的代码不起作用的两个可能原因:

  • 您提供的示例swagger文件无效。开始时至少应该还有一个
    {
    。因为
    $.ajax
    会自动尝试解析,所以需要确保接收到的数据是有效的JSON。 但是可以阻止$.ajax解析JSON。为此,您需要向$.ajax的设置对象添加
    数据类型:“text”

  • 使用localhost会导致问题,因为安全设置不同于 访问internet上的普通网站。因此,我建议您使用 您的计算机和本地主机的名称

  • 为了找出问题所在,您需要查看错误文本。更改代码如下:
    console.log(err.statusText)

    您需要确定此错误是发生在客户端还是服务器端。请查看服务器的日志文件。是否可以看到您的请求?http状态代码是什么

    下一步:在google chrome/devopler工具中,查看网络选项卡。您能看到请求和响应吗?双击可查看所有详细信息。您能看到服务器的响应吗


    然后在服务器上,您应该尝试将日志记录级别更改为跟踪甚至调试。这允许您查看请求的所有详细信息。

    这是因为我的javascript中没有阻止提交功能。
    $('#form').submit(函数(e){e.preventDefault();});


    一旦添加了这个,页面就工作了。

    我仔细检查了json招摇过市。我在上的招摇过市编辑器中使用了yaml并下载了json,这就是它的生成方式。我添加了数据类型:“text”,将localhost更改为我的机器名,将console.log更改为err.statusText。我得到的只是“error”一词。我能够获得curl并运行curl命令,它就可以工作了。此时我唯一无法工作的是ajax调用,它会出错,这是唯一给出的信息。我终于解决了问题。我需要添加我的javascript中的一个防止默认提交的函数。