[英]Ajax call successful, but error block is executing
我正在嘗試將帖子保存到我的數據庫中,但是正在執行ajax錯誤塊。 盡管它在響應中說還不錯,所以我環顧了其他一些問題,似乎我沒有返回Json對象,所以嘗試了一些方法:
創建一個NameValule變量並添加(“ Success”,“ true”)並將其通過Json.Encode(NameValue)轉換為Json;
返回Json格式的字符串:“ {\\” Result \\“:[{\\” Success \\“:\\” true \\“}]}” ;;
將dataType更改為“ text json”“ text / json”
由於某些原因,錯誤塊仍然執行,有什么想法嗎?
//Save it all in an object to be passed in ajax
var Post = {
Id: postID,
Title: postTitle.val(),
Author: postAuthor,
Date: postDate,
Content: postContent.val(),
metaDescription: metaDescription.val(),
metaKeywords: metaKeywords.val(),
metaID: metaId.text(),
Page: $(document).attr('title')
};
//save to database
$.ajax({
url: url,
type: 'POST',
dataType: "text json",
data: { data: JSON.stringify(Post) },
success: function (result) {
console.log("result: " + result);
if (result == "Success") {
// postContent.append("<br/><p>Edit Successful!</p>");
alert('Edit successfull');
//window.location.replace(window.location.href);
}
else {
postContent.replaceWith("<div>" + result + "</div>");
}
},
error: function (xhr, status) {
console.log('ajax error = ' + xhr.statusText);
}
});
這是響應頁面:
@using WebMatrix.Data;
@functions{
public string EditPost()
{
var db = Database.Open("StarterSite");
var post = Request.Unvalidated["data"];
var result = Json.Decode(post);
var error = new System.Collections.Specialized.NameValueCollection();
/* Id: postID,
Title: postTitle,
Author: postAuthor,
Date: postDate,
Content: afterEdit,
Page: $(document).attr('title')
*/
if(string.IsNullOrEmpty(result["Id"]))
{
error.Add("Error", "Id empty");
return Json.Encode(error);
}
if (string.IsNullOrEmpty(result["Author"]))
{
error.Add("Error", "Author empty");
return Json.Encode(error);
}
if (string.IsNullOrEmpty(result["Content"]))
{
error.Add("Error", "Content empty");
return Json.Encode(error);
}
if (string.IsNullOrEmpty(result["Date"]))
{
error.Add("Error", "Date empty");
return Json.Encode(error);
}
//Page and Title only ones that can be empty
var cmd = "UPDATE Posts SET ID='" + result["Id"]
+ "',Author='" + result["Author"]
+ "',Content='" + result["Content"]
+ "',Date='" + result["Date"]
+ "',Title='" + result["Title"]
+ "',Page='" + result["Page"]
+ "' WHERE ID='" + result["Id"] + "';";
try { db.Execute(cmd); }
catch (Exception e)
{
error.Add("Error",e.Message);
return Json.Encode(error);
}
if (string.IsNullOrEmpty(result["metaDescription"]))
{
error.Add("Error", "metaDescription empty");
return Json.Encode(error);
}
if (string.IsNullOrEmpty(result["metaKeywords"]))
{
error.Add("Error", "metaKeywords empty");
return Json.Encode(error);
}
//Post was edited successfully add/update meta info
int parseResult = 0;
Int32.TryParse(result["metaID"], out parseResult);
if (parseResult > 0)//metaID is supplied
{
cmd = "UPDATE MetaInfo SET Description='" + result["metaDescription"]
+ "',Keywords='" + result["metaKeywords"]
+ "',postID='" + result["Id"]
+ "' WHERE ID='" + result["metaID"] + "';";
}
else //metaID is not supplied
{
cmd = "INSERT INTO MetaInfo (Description,Keywords,postID) VALUES('"
+ result["metaDescription"] + "','"
+ result["metaKeywords"] + "','"
+ result["Id"] + "');";
}
try
{
db.Execute(cmd);
}
catch (Exception e)
{
error.Add("Error",e.Message);
return Json.Encode(error);
}
//End Update meta info
error.Add("Success", "true");
return Json.Encode(error); //"{ \"Result\":[{\"Success\":\"true\"}]}";
}
}
@{
var result = EditPost();
}
@result
不知道網址的要求就無法確定。 我的猜測是data屬性是您離開的原因。 我猜Post
是在其他地方定義的對象。 發送請求時,服務器可能會將數據字段解釋為對象而不是字符串。 嘗試將{data: JSON.stringify(Post)}
更改為JSON.stringify(Post)
嘗試添加async:true,在類型之后輸入:“ POST”,
問題出在您的JSON響應上。
"{ \"Result\":[{\"Success\":\"true\"}]}";
在雙引號(“)之前添加了斜杠。
您可以使用以下更新的錯誤塊查看jQuery拋出的實際錯誤:
error: function (xhr, status, errorThrown ) {
console.log(errorThrown )
解:
從您的響應嘗試響應中刪除斜杠,如下所示:
'{ "Result":[{"Success":"true"}]}'
希望這會幫助你。
問候,
對於其他可能遇到相同問題的人,這是我最終解決的方法:只需使用Html.Raw(result)僅發送回json即可,而不發送由於某種原因而添加的多余內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.