日期:2014-05-17  浏览次数:20433 次

Jquery ajax get参数怎么写?
下面的是WCF契约
C# code
[OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, UriTemplate = "GetContactsByGroupID/{groupID}", ResponseFormat = WebMessageFormat.Json)]
        List<ContactsDetail> GetContactsByGroupID(string groupID);


下面的调用示例
C# code
http://localhost:7941/AddressBookService/GetContactsByGroupID/51E9DC73-E284-4074-B04A-EED95A8711DB


小弟不才,对UI这块确实没学过,摸着石头过河,搞了半天也没搞对,求指点,在线等
JScript code
$.ajax({
                url: 'http://localhost:7941/AddressBookService/GetContactsByGroupID',
                data: { groupID: '51E9DC73-E284-4074-B04A-EED95A8711DB'},
                type: 'GET',
                dataType: 'jsonp',
                error: function () {
                    alert('Error!');
                },
                success: function (allContacts) {TODO}


------解决方案--------------------
url: 'http://localhost:7941/AddressBookService/GetContactsByGroupID/51E9DC73-E284-4074-B04A-EED95A8711DB',
type: 'GET',
------解决方案--------------------
准备工作
·Customer类

public class Customer
{
public int Unid { get; set; }
public string CustomerName { get; set; }
public string Memo { get; set; }
public string Other { get; set; }
}

 
·服务端处理(Json_1.ashx)
Customer customer = new Customer 
{ Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);

context.Response.Write(strJson);
 
(一)Jquery. getJSON
方法定义:jQuery.getJSON( url, data, callback )
通过get请求得到json数据
·url用于提供json数据的地址页
·data(Optional)用于传送到服务器的键值对
·callback(Optional)回调函数,json数据请求成功后的处理函数
function(data, textStatus) {
// data是一个json对象
// textStatus will be "success"
this; // the options for this ajax request
}
 
(1)一个对象
$.getJSON(
"webdata/Json_1.ashx",
function(data) {
$("#divmessage").text(data.CustomerName);
}
);
 
向Json_1.ashx地址请求json数据,接收到数据后,在function中处理data数据。 这里的data的数据是一条记录,对应于一个customer实例,其中的数据以k/v形式存在。即以[object,object]数组形式存在。
{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}
所以在访问时,以data.Property来访问,下面以k/v循环来打印这条宋江的记录:

$.getJSON(
"webdata/Json_1.ashx",
function(data) {
var tt="";
$.each(data, function(k, v) {
tt += k + ":" + v + "<br/>";
})
$("#divmessage").html(tt);
});

 
结果:
Unid:1
CustomerName:宋江
Memo:天魁星
Other:黑三郎