【问题标题】:jquery ajax call returning error when response text is string响应文本为字符串时,jquery ajax调用返回错误
【发布时间】:2012-07-10 10:41:26
【问题描述】:

我正在使用以下详细信息发出 ajax 请求:

Request URL:http://localhost:8080/MyWebService/cars/70ced046-3061-4d9d-b9ce-7d1291d5b5c0
Request Method:DELETE
Status Code:200 OK

Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Proxy-Connection:keep-alive
Referer:http://localhost:8080/MyWebService/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)     Chrome/20.0.1132.47 Safari/536.11
X-Requested-With:XMLHttpRequest

Response Headers
Content-Length:36
Content-Type:application/json
Date:Tue, 10 Jul 2012 10:18:45 GMT
Server:Apache-Coyote/1.1

但随后 ajax 调用返回错误并带有以下 xhr、状态和错误对象:

function(request, status, error) 

>status
"parsererror"

>error
SyntaxError
arguments: Array[0]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "unexpected_token_number"
__proto__: Error

>request
Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
isRejected: function () {
isResolved: function () {
overrideMimeType: function ( type ) {
pipe: function ( fnDone, fnFail, fnProgress ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseText: "44550569-871d-49ae-8583-f07ee3a07832"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( doneCallbacks, failCallbacks, progressCallbacks ) {
__proto__: Object

如果响应是整数或任何其他 json,则调用可以正常进入成功函数。但如果响应是字符串 (44550569-871d-49ae-8583-f07ee3a07832) 则会出错。 有关如何解决此问题的任何建议?

【问题讨论】:

    标签: ajax jquery


    【解决方案1】:

    因为您希望响应是 JSON

    Content-Type:application/json
    

    粘贴您的 ajax 调用 javascript 部分,以便我可以为您提供更多信息。

    【讨论】:

    • 我没有通过 ajax 调用强制响应为 application/json,而是在服务器端。谢谢你指引我正确的方向。
    【解决方案2】:

    问题出在我强制服务器返回 application/json 的服务器端:

    @RequestMapping(method = RequestMethod.DELETE, value = "/cars/{id}", produces="application/json")
    

    产生以下响应头:

    Response Headers
    Content-Length:36
    Content-Type:application/json
    Date:Tue, 10 Jul 2012 10:18:45 GMT
    Server:Apache-Coyote/1.1
    

    现在我删除了 "produces="application/json"" 部分,它返回以下标题:

    Content-Length:36
    Content-Type:text/plain;charset=ISO-8859-1
    Date:Wed, 11 Jul 2012 06:08:00 GMT
    Server:Apache-Coyote/1.1
    

    【讨论】:

    • 现在一切都解决了。对?但请记住,现在您期望纯文本作为响应。因此,如果它的 json,最好在使用它之前解析它 - jQuery.parseJSON( json )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2014-09-15
    • 2014-01-03
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多