Jquery ajax get参数怎么写?

苏二 2012-04-20 08:34:44
下面的是WCF契约
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, UriTemplate = "GetContactsByGroupID/{groupID}", ResponseFormat = WebMessageFormat.Json)]
List<ContactsDetail> GetContactsByGroupID(string groupID);

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


小弟不才,对UI这块确实没学过,摸着石头过河,搞了半天也没搞对,求指点,在线等
$.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}
...全文
634 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kankankankan2222 2012-04-21
  • 打赏
  • 举报
回复
准备工作
·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:黑三郎
苏二 2012-04-20
  • 打赏
  • 举报
回复
求支援
苏二 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

url: 'http://localhost:7941/AddressBookService/GetContactsByGroupID/51E9DC73-E284-4074-B04A-EED95A8711DB',
type: 'GET',
[/Quote]
我知道这种方法可以解决,但是不知道您能看懂我的思路,按照这种方式请求,因为到时候还有post传值````
孟子E章 2012-04-20
  • 打赏
  • 举报
回复
url: 'http://localhost:7941/AddressBookService/GetContactsByGroupID/51E9DC73-E284-4074-B04A-EED95A8711DB',
type: 'GET',

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧