我正在尝试从ashx文件中的jquery ajax获取json数据,但结果在某种程度上无法使用。我怎样才能将它绑定到动态或替代?
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var result = new StreamReader(context.Request.InputStream).ReadToEnd();
}
结果
maintype = Entity.JobApplication&安培;馈送=安培;字段%5B0%5D%5Bkey%5D = EmployeeRequest&安培;字段%5B0%5D%5Bvalue%5D = 1&安培;字段%5B1%5D%5Bkey%5D =状态&安培;字段%5B1%5D%5Bvalue%5D = 1&安培;字段%5B2%5D%5Bkey%5D =姓&安培;字段%5B2%5D%5Bvalue%5D = 11&安培;字段%5B3%5D%5Bkey%5D =名字&安培;字段%5B3 %5D%5Bvalue%5D = 22
**json data**
var data = {
"maintype": "Entity.JobApplication",
"feeds": "",
"fields": [
{
"key": "EmployeeRequest",
"value": ""
},
{
"key": "State",
"value": ""
},
{
"key": "FirstName",
"value": ""
},
{
"key": "LastName",
"value": ""
}
]
};
var data = <%=AjaxJSON%>;
for (var i in data.fields)
{
var o = $("[prop=" + data.fields[i].key + "]");
data.fields[i].value = o.val();
}
genericAjax("/_Handler/CreateEntity.ashx", data);
答案 0 :(得分:0)
您需要使用WebUtility.UrlDecode
解码字符串。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var result =WebUtility.UrlDecode(new StreamReader(context.Request.InputStream).ReadToEnd());
}
结果:
maintype = Entity.JobApplication&安培;馈送=安培;字段[0] [键] = EmployeeRequest&安培;字段[0] [值] = 1&安培;字段[1] [键] =状态&安培;字段[1] [值] = 1&安培;领域[2] [键] =姓&安培;领域[2] [值] = 11&安培;字段[3] [键] =名字&安培;字段[3] [值] = 22
顺便说一下,你得到的数据不是Json就绪格式。
在这里你可以解析它更接近json(添加引用:System.Web,System.Web.Extension):
string s = "maintype=Entity.JobApplication&feeds=&fields%5B0%5D%5Bkey%5D=EmployeeRequest&fields%5B0%5D%5Bvalue%5D=1&fields%5B1%5D%5Bkey%5D=State&fields%5B1%5D%5Bvalue%5D=1&fields%5B2%5D%5Bkey%5D=FirstName&fields%5B2%5D%5Bvalue%5D=11&fields%5B3%5D%5Bkey%5D=LastName&fields%5B3%5D%5Bvalue%5D=22";
var dict = System.Web.HttpUtility.ParseQueryString(s);
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(
dict.AllKeys.ToDictionary(k => k, k => dict[k]));
你会得到:
{ “maintype”: “Entity.JobApplication”, “进料”: “”, “字段[0] [键]”: “EmployeeRequest”, “字段[0] [值]”: “1”,”字段[1] [键] “:” 国”, “字段[1] [值]”: “1”, “字段[2] [键]”: “姓”, “字段[2] [值]” : “11”, “字段[3] [键]”: “名字”, “字段[3] [值]”: “22”}
再做几步就可以将它转换为json。
答案 1 :(得分:0)
我使用JSON.stringify(data)
genericAjax("/_Handler/CreateEntity.ashx", JSON.stringify(data));