2012-07-10 84 views
0

我正在跟以下細節的Ajax請求:jQuery的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),則會發生錯誤。 關於如何解決這個問題的任何建議?

回答

0

問題是在服務器側,其中i是迫使服務器返回應用程序/ 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 

現在我刪除「生產=「應用程序/ 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 
+0

現在一切都是固定的。對?但請記住,現在您期望純文本作爲響應。所以,如果它的json,使用它之前更好地解析它 - jQuery.parseJSON(json) – Jithin 2012-07-11 06:29:21

+0

是的,我會的。謝謝 :) – nilgun 2012-07-11 08:17:50

1

因爲你期望的響應是一個JSON

Content-Type:application/json 

粘貼你的Ajax調用JavaScript的一部分,所以我可以爲您提供更多的信息。

+0

我不是強迫響應被應用on/json通過ajax調用,但在服務器端。感謝您指導我朝着正確的方向發展。 – nilgun 2012-07-11 06:27:53

相關問題