我嘗試在ASP.NET MVC 4中對服務器端方法進行簡單調用。我通過對方法進行jQuery調用來做到這一點。該方法被調用且屬性正確,但此方法的響應不正確。我嘗試使用JSON.NET序列化.NET對象。JsonResult不返回(有效)json - 序列化出錯?
我可以看到我獲得以下響應(使用Firebug):
"{\"VisitorId\":\"11a0606b-5336-4fa7-b50f-3edf97d8301b\",\"PhoneToTransfer\":\"61793650\",\"PhoneToNotify\":\"\",\"EmailToNotify\":\"[email protected]\",\"EmailToTransfer\":\"\",\"OrderState\":\"PaymentPending\",\"DestinationAddress\":\"1Ccypfi3rnXosUgY6p1sQVXFyddFvwLFEJ\",\"TransactionHash\":\"\",\"Value\":12.0,\"Confirmations\":-1,\"TransactionDate\":\"2013-01-09T22:36:33.7991116+01:00\"}"
這並不解析爲正確的JSON,我不知道爲什麼。此外,Firebug中的「JSON」選項卡顯示「沒有屬性可顯示此對象」。
我很確定我的內容類型和數據類型在調用中是正確的。
這是我的代碼,以使JSON - 使用JSON.NET庫:
[HttpPost]
public JsonResult StartPurchase(string phoneReceiver, string emailNotify, string value)
{
if (ModelState.IsValid)
{
BlockchainPayments block = new BlockchainPayments();
var address = block.GetAddress(BtcContext.PurchaseSession);
decimal val = 0;
decimal.TryParse(value, out val);
var ps = new PurchaseService();
var purchase = ps.StartPurchase(BtcContext.PurchaseSession, phoneReceiver, emailNotify, address.destination, val);
return Json(JsonConvert.SerializeObject(purchase));
}
return Json("Error");
}
這是jQuery的電話:
$.ajax({
type: "POST",
url: url,
data: dataToSend,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert('Awesome destination succes');
},
error: function (date) {
alert('An occurred while purchasing. Please try again later');
}
});
任何想法,爲什麼它沒有返回有效的JSON?有關如何解決它的任何想法?
* Stackoverflow高效* - 解決了問題..現在,睡覺,並得到一些新鮮的眼睛。 非常感謝! –