Post Json Data by Ajax, and Parse the Json Data in backend

前几天,项目需要在后台接受Posted json data。于是我做了一个简单的json发送页面如下:

页面上:

    <div>
        <input id="btnSentJson" type="button" value="Post Json" />
    </div>

JS处理:
$.ajaxSetup({ cache: false });
$(function() {
    $("#btnSentJson").click(
        function() {
            PostJson();
        }
    )
}
);

function PostJson() {
    var jsont = { "pt": "app", "lg": "en", "OS": "and2.2", "pn": 9998, "CV": "0001000400040107" };
    alert(JSON.stringify(jsont));
    $.ajax(
     {
         url: "http://localhost:1470/EProduct.aspx?json=true",
         type: "POST",
         dataType: "json",
         //processData: false,
         data: JSON.stringify(jsont),//jsont, 
         contentType: "application/json; charset=utf-8",

         success: function(theback) {
         alert(JSON.stringify(theback));
         },
         error: function(error) {
         alert(error.status + ";;" + error.responseText);
         }
     });
 }

因为默认浏览器是firefox,所以每次都在firefox下执行。在目标程序都能接收到Post的请求,但没有内容。Request,Form,inputstream全部都是空的。我上网查了很多资料,试过了无限可能,可惜除了被误导还是被忽悠。始终没有解决问题。我尝试用firefox的Poster工具,最终实现了后台的处理:

                    if (Request.InputStream != null && Request.InputStream.Length > 0)
                    {
                        byte[] byts = new byte[Request.InputStream.Length];
                        Request.InputStream.Read(byts, 0, byts.Length);
                        string JsonInfo = System.Text.Encoding.Default.GetString(byts);
                        JsonInfo = Server.UrlDecode(JsonInfo);

                        JsonInput jiObj = JSONSerializer.Deserialize<JsonInput>(JsonInfo);

其中的JsonInput是自定义的一个类,定义了各个传入的参数。

但最终,我的发送程序还是不能奏效啊,百思不得其解之间,居然看到一种可能就是firefox不支持Option意外的传送方式。
type: "POST",

无论type定义成什么,最终都被解释为Option。我在后台调试的时候特意留意了一下type,果然是Option。于是将浏览器改为IE再次尝试,居然神奇的通过了。

因为这么一个firefox的问题,让我纠结了这么久。伤不起啊。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值