ASMX Web服务,JSON的JavaScript / jQuery的? [英] asmx web service, json, javascript/jquery?

查看:190
本文介绍了ASMX Web服务,JSON的JavaScript / jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASMX检索数据库的一些数据,

I am using asmx to retrieve some data from DB,

public class TestPage1
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}




    [WebMethod]
    public EntityLayer.TestPage1 GetData(int id)
    {
        TestPage1 test = TestPage1.GetData(id).SingleOrDefault();
        return test;
    }


$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/GetData",
  data: "{id}",
  dataType: "json"
});

如何desrialize测试对象的JavaScript?
 并有更好的办法?
谢谢

How Do I desrialize test object in javascript?? and is there a better way? thanks

推荐答案

我建议你看看我的previous答案收盘问题<一href=\"http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086\">How做我建一个JSON对象发送到一个AJAX web服务?和<一个href=\"http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583\">Can我来自的.asmx Web服务返回JSON如果将contentType不是JSON?

I recommend you look my previous answer for the close questions How do I build a JSON object to send to an AJAX WebService? and Can I return JSON from an .asmx Web Service if the ContentType is not JSON?

正确的code应该看起来像下面的

The correct code should looks like following

[WebMethod]
[ScriptMethod (ResponseFormat = ResponseFormat.Json)]
public EntityLayer.TestPage1 GetData(int id)
{
    TestPage1 test = TestPage1.GetData(id).SingleOrDefault();
    return test;
}

var myData = 5;
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "WebService.asmx/GetData",
    //data: {id:JSON.stringify(myData)},
    data: JSON.stringify({id:myData}),
    dataType: "json",
    success: function(response){
        alert("UserName=" + response.d.UserName +
              ", FirstName=" + response.d.FirstName +
              ", MiddleName=" + response.d.MiddleName+
              ", LastName=" + response.d.LastName);
    }
})

其中, JSON.stringify 是从脚本json2.js你可以从的 http://www.json.org/js.html

where JSON.stringify is a function from the script json2.js which you can download from http://www.json.org/js.html.

如果在 ID 的值是整数 JSON.stringify(MYDATA的)相同 myData的,但对于所有的更复杂的例子我严格推荐您使用此功能。

If the id values are integer JSON.stringify(myData) are the same as myData, but for all more complex examples I strictly recommend you to use this function.

你怎么还可以从$ C $看c中的Web方法的所有结果将保存在属性 D ,所以你应该例如<$使用C $ C> response.d.FirstName 语法来访问的第一个名字。

How you can also see from the code the all results of the web method will be saved in the property d, so you should use for example response.d.FirstName syntax to access the first name.

更新时间::在HTTP的情况下,获得数据参数应该是 {ID:JSON.stringify(MYDATA的) } 。在HTTP POST的情况: JSON.stringify({ID:myData的})

UPDATED: In case of HTTP GET the data parameter should be {id:JSON.stringify(myData)}. In case of HTTP POST: JSON.stringify({id:myData})

这篇关于ASMX Web服务,JSON的JavaScript / jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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