JsonResult不返回(有效)JSON-序列化出错? [英] JsonResult not returning (valid) json - Serializing going wrong?

查看:328
本文介绍了JsonResult不返回(有效)JSON-序列化出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在ASP.NET MVC 4中对服务器端方法进行简单调用.我通过对方法进行jQuery调用来实现此目的.该方法被调用并且属性正确,但是此方法的响应不正确.我尝试使用JSON.NET序列化.NET对象.

I try to make a simple call to a server-side method in ASP.NET MVC 4. I do this by making a jQuery call to a method. The method gets called and the properties are correct, but the response of this method is not correct. I try to serialize an .NET object using JSON.NET.

我可以看到我收到以下响应(使用FireBug):

I can see I get the following response (using FireBug):

"{\"VisitorId\":\"11a0606b-5336-4fa7-b50f-3edf97d8301b\",\"PhoneToTransfer\":\"61793650\",\"PhoneToNotify\":\"\",\"EmailToNotify\":\"mcoroklo@gmail.com\",\"EmailToTransfer\":\"\",\"OrderState\":\"PaymentPending\",\"DestinationAddress\":\"1Ccypfi3rnXosUgY6p1sQVXFyddFvwLFEJ\",\"TransactionHash\":\"\",\"Value\":12.0,\"Confirmations\":-1,\"TransactionDate\":\"2013-01-09T22:36:33.7991116+01:00\"}"

这不能解析为正确的JSON,我也不知道为什么.另外,Firebug的"JSON"选项卡显示此对象没有要显示的属性".

This doesn't parse as correct JSON, and I have no idea why. Also, the "JSON" tab in Firebug says "There are no properties to show for this object".

我很确定我的内容类型和数据类型在通话中是正确的.

I am pretty sure my content-type and dataType is correct on the call.

这是我制作JSON的代码-使用JSON.NET库:

This is my code to make the JSON - using The JSON.NET library:

[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调用:

This is the jQuery call:

$.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吗?还有解决问题的想法吗?

Any idea why it's not returning valid JSON? And any ideas on how to solve it?

推荐答案

您似乎对它进行了双重编码,只需使用一种转换方法,例如return Json(purchase);

It looks like you are double encoding it, just use one conversion method, e.g. return Json(purchase);

这篇关于JsonResult不返回(有效)JSON-序列化出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆