2014-02-24 1 views
0

jqXHR 객체에 추가 필드를 전달할 방법이 있습니까? 여기 내 상황 :AJAX jqXHR에 사용자 정의 필드 전달

내가 돌아 해요 HttpResponseMessage과 같이 :

response.ReasonPhrase = "someString " + Resources.Messages.NoData; 
response.StatusCode = HttpStatusCode.NoContent; 
return response; 

하지만 다른 이유로, 다른 StatusCode = HttpStatusCode.NoContent;을 통과 할 몇 가지 경우가 같습니다 때문에

response.ReasonPhrase = "anotherString " + Resources.Messages.NoMoreData; 
response.StatusCode = HttpStatusCode.NoContent; 
return response; 

가 메시지가 모두 사용자 언어로 현지화되었으므로 (Resources.Messages) "someString " 또는 "anotherString " 또는 AJAX의 "...."과 같으면 추가 필드가 필요합니다. 다시통화 :

이상한 것은이
$.ajax({ 
type: 'POST', 
url: '/api/testing/test', 
data: jobject, 
contentType: 'application/json', 
success: function (data, status, xhr) { 
    if (status == "nocontent" && xhr.statusText.startsWith("someString")) { 
     // do something 
    } 
    if (status == "nocontent" && xhr.statusText.startsWith("anotherString")) { 
     // do something 
    } 

error: function (xhr) { 
    debugger; 
    if (xhr.responseJSON == 'UnableToParseDateTime') { 
     // do something 
    } 
} 
}); 

, xhr.statusTextxhr.statusText.startsWith()을 지원하지 않는다는 것입니다. 따라서 "someString " 또는 "anotherString " 또는 "...."에 대한 등호 검사가 작동하지 않습니다.

나는 responseText이 (가) success 콜백에서 항상 비어있는 것으로 나타났습니다. 만약 내가 '채울 수'그 서버에서 좋은 것입니다. 그렇지 않다면 jqXHR 개체에 대한 추가 필드를 어떻게 가질 수 있습니까? 또는 몇 가지 새로운 사용자 정의 HttpStatusCode을 정의 할 수 있습니까? startsWith 표준 자바 스크립트의 경우

답변

1

는 잘 모르겠지만, 당신은 항상 (this SO 참조) 문자열의 프로토 타입에 자신의 기능을 추가 할 수 있습니다

if (typeof String.prototype.startsWith != 'function') { 
    String.prototype.startsWith = function(str) { 
    return this.lastIndexOf(str, 0) === 0; 
    }; 
} 

당신이 startsWith 어떤 문자열을 확인 할 수있다. jqXHR 객체에 추가하는 방법에 대한 질문에 도움이


, 당신은 docs를 살펴 보셨나요? AJAX 호출의 beforeSend 함수에서 jqXHR 객체를 수정할 수 있습니다.

+0

asp.net-webapi를 사용하여 jqXHR에 사용자 정의 필드의 예가 있습니까? 나는 예. 그렇지만 AJAX에 관한 모든 것을 읽지는 않습니다. – Quoter