结果被示为空 [英] Result is shown as empty

查看:99
本文介绍了结果被示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户创建 JSON对象,它有如下值:

<$p$p><$c$c>{"Title":"Mr","FirstName":"S","LastName":"J","Birthday":"01/01/2011","Address":[{"Line1":"Line1","Line2":"Line2","City":"City","State":"State","Zip":"00000","County":"0000"},{"Line1":"Line11","Line2":"Line21","City":"City1","State":"State1","Zip":"11111","County":"1111"}],"Email":[{"Email":"s.j@sj.com","EmailType":"Personal"},{"Email":"s.j1@company.com","EmailType":"Work"}],"Phone":[{"Phone":"1231231234","PhoneType":"Mobile"},{"Phone":"1231232345","PhoneType":"Work"}]}

我需要在处理程序中该数据/ CustomerHandler.ashx做一些数据库操作。 我的AJAX调用如下:

  $。阿贾克斯({
        键入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        网址:处理程序/ CustomerHandler.ashx
        数据:客户,
        数据类型:JSON,
        成功:insertCustomerCallback
    });
 

昏暗customerJSON的String = HttpContext.Current.Request.Form(客户)显示为空。

解决方案

您可以发送客户为使用JSON.stringify方法从的 json2.js

  VAR的客户= {标题:先生,姓:S,姓氏:J,生日:01/01/2011 ,地址:[{1号线:1号线,2号线:2号线,城市:城市,国家:国家,邮编:00000,县:0000},{线路1:Line11,2号线:Line21,城市:City1,国家:状态1,邮编:11111,县:1111}],电子邮件:[{电子邮件:sj@sj.com,EmailType:个人},{电子邮件:s.j1@company.com EmailType:工作}],手机:[{手机:1231231234,PhoneType:手机},{手机:1231232345,PhoneType:工作} ]};
$阿贾克斯({
    键入:POST,
    的contentType:应用/ JSON的;字符集= UTF-8,
    网址:处理程序/ CustomerHandler.ashx
    数据:JSON.stringify(客户),
    数据类型:JSON,
    成功:函数(结果){

    }
});
 

和在通用处理器从请求流中读取它:

 暗淡客户=新的字节(context.Request.InputStream.Length  -  1){}
context.Request.InputStream.Read(客户,0,customer.Length)
昏暗customerJSON = Encoding.UTF8.GetString(客户)
// TODO:反序列化JSON回Customer对象
 

正如你可以使用脚本替代启用的WebMethod

I have a Customer JSON object created which has value as below:

{"Title":"Mr","FirstName":"S","LastName":"J","Birthday":"01/01/2011","Address":[{"Line1":"Line1","Line2":"Line2","City":"City","State":"State","Zip":"00000","County":"0000"},{"Line1":"Line11","Line2":"Line21","City":"City1","State":"State1","Zip":"11111","County":"1111"}],"Email":[{"Email":"s.j@sj.com","EmailType":"Personal"},{"Email":"s.j1@company.com","EmailType":"Work"}],"Phone":[{"Phone":"1231231234","PhoneType":"Mobile"},{"Phone":"1231232345","PhoneType":"Work"}]}

I need to get this data in the Handlers/CustomerHandler.ashx to do some DB operations. My AJAX call is as follows:

  $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Handlers/CustomerHandler.ashx",
        data: Customer,
        dataType: "json",
        success: insertCustomerCallback
    });

Dim customerJSON As String = HttpContext.Current.Request.Form("Customer")is shown as empty.

You might send the customer as a JSON object using the JSON.stringify method from json2.js:

var Customer = { "Title": "Mr", "FirstName": "S", "LastName": "J", "Birthday": "01/01/2011", "Address": [{ "Line1": "Line1", "Line2": "Line2", "City": "City", "State": "State", "Zip": "00000", "County": "0000" }, { "Line1": "Line11", "Line2": "Line21", "City": "City1", "State": "State1", "Zip": "11111", "County": "1111"}], "Email": [{ "Email": "s.j@sj.com", "EmailType": "Personal" }, { "Email": "s.j1@company.com", "EmailType": "Work"}], "Phone": [{ "Phone": "1231231234", "PhoneType": "Mobile" }, { "Phone": "1231232345", "PhoneType": "Work"}] };
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Handlers/CustomerHandler.ashx",
    data: JSON.stringify(Customer),
    dataType: "json",
    success: function (result) {

    }
});

and on the generic handler read it from the request stream:

Dim customer = New Byte(context.Request.InputStream.Length - 1) {}
context.Request.InputStream.Read(customer, 0, customer.Length)
Dim customerJSON = Encoding.UTF8.GetString(customer)
// TODO: deserialize the JSON back to a Customer object

As an alternative you could use a script enabled WebMethod.

这篇关于结果被示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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