ajax帖子命中服务器并提供正确的信息,但即使它有效,它仍然会遇到错误功能。就绪状态为0,因此它表示它甚至没有发出请求。
var serviceURL = '/ContactForm/FirstAjax';
$.ajax({
type: "POST",
url: serviceURL,
data: JSON.stringify(formInfo),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Worked");
},
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process correctly, please try again" + "\n xhRequest: " + xhRequest + "\n" + "ErrorText: " + ErrorText + "\n" + "thrownError: " + thrownError);
}
});
错误消息为:
我的控制器看起来像这样:
[HttpPost]
[ActionName("FirstAjax")]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public JsonResult FirstAjax(ContactForm contactForm)
{
return Json("works", JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:0)
你的ajax post方法或你的控制器有错误。 如果按原样保留ajax方法,则可以将控制器修改为:
[HttpPost]
[ActionName("FirstAjax")]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public JsonResult FirstAjax()
{
var postedData = new StreamReader(Request.InputStream);
var jsonEncoded = postedData.ReadToEnd(); //String with json
//Decode your json and do work.
return Json("works", JsonRequestBehavior.AllowGet);
}
如果您不想更改控制器,则需要将javascript更改为:
var serviceURL = '/ContactForm/FirstAjax';
$.ajax({
type: "POST",
url: serviceURL,
data: { contactForm: JSON.stringify(formInfo) },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Worked");
},
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process correctly, please try again" + "\n xhRequest: " + xhRequest + "\n" + "ErrorText: " + ErrorText + "\n" + "thrownError: " + thrownError);
}
});
希望这有帮助。
答案 1 :(得分:0)
我正在使用Fiddler并意识到它正在执行GET
而不是POST
。我想出了如何修复它。当我提交表单时,它使用onsubmit="submitForm();"
调用Javascript。通过提交表单,它会在网址上预先形成GET
,而不是POST
。我修复了onsubmit="submitForm(); return false"
。