ASP.NET MVC 앱의 jQuery ajax parsererror
-
18-09-2019 - |
문제
JQuery Ajax 메소드를 통해 ASP.NET MVC ActionMethod를 호출하려고합니다. 내 코드는 다음과 같습니다.
$('.Delete').live('click', function() {
var tr = $(this).parent().parent();
$.ajax({
type: 'DELETE',
url: '/Routing/Delete/' + tr.attr('id'),
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + textStatus + " " + errorThrown);
alert(XMLHttpRequest.getAllResponseHeaders());
},
success: function(result) {
// Remove TR element containing waypoint details
alert("Success");
$(tr).remove();
}
});
});
그리고 내 행동 방법은 다음과 같습니다.
[AcceptVerbs(HttpVerbs.Delete)]
public string Delete(int id)
{
// Deletion code
return " ";
}
컨텐츠 길이가 0이면 문제를 일으킬 수있는 곳을 읽을 때 빈 문자열을 반환합니다. 반환 유형이 문자열 일 때 "오류 : 오류 미정"이라는 경고 상자가 발생하고 두 번째 알림 상자가 비어 있습니다.
반환 유형을 무효화하면 "ERROR : PARSERERROR UNDEFINED"라는 경고가 표시되고 두 번째 경고는 다음과 같습니다.
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 22 Jul 2009 08:27:20 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Length: 0
Connection: Close
해결책
나는 빈 문자열을 반환하는 것을 권장하지 않습니다. 데이터 타입을 JSON jQuery로 설정하면 응답을 평가합니다.
항상 논리적 메시지를 반환해야합니다.
return Json(new { success = "true" });
NB 사용중인 성공 $(tr).remove()
; TR 변수가 이미 jQuery 객체이므로 필요하지 않습니다.tr.remove
잘 작동합니다.
다른 팁
jQuery 호출은 요청에 대한 대가로 JSON을 기대합니다. 그래서 :
[AcceptVerbs(HttpVerbs.Delete)]
public JsonResult Delete(int id) {
// Deletion code
return Json("");
}
또한 동의합니다 붉은 광장, 다음과 같은 논리적 메시지를 반환하는 것이 좋습니다.
[AcceptVerbs(HttpVerbs.Delete)]
public JsonResult Delete(int id) {
// Deletion code
return Json(new { Success = true });
}
//then in your jQuery function you can check the result this way :
success: function(result) {
if (result.Success) {
alert("it was deleted!");
}
else {
alert("something went wrong");
}
}
제휴하지 않습니다 StackOverflow