This question already has an answer here:
这个问题已经有了答案:
- How to use jQuery to call an ASP.NET web service? 7 answers
- 如何使用jQuery调用ASP。净web服务?7的答案
I want to call a webservice from jQuery. How can I do that?
我想从jQuery调用一个web服务。我怎么做呢?
5 个解决方案
#1
29
You can make an AJAX request like any other requests:
可以像其他请求一样发出AJAX请求:
$.ajax( {
type:'Get',
url:'http://mysite.com/mywebservice',
success:function(data) {
alert(data);
}
})
#2
7
EDIT:
编辑:
The OP was not looking to use cross-domain requests, but jQuery supports JSONP as of v1.5. See jQuery.ajax(), specificically the crossDomain
parameter.
OP没有考虑使用跨域请求,但是jQuery在v1.5中支持JSONP。参见jQuery.ajax(),具体来说是跨域参数。
The regular jQuery Ajax requests will not work cross-site, so if you want to query a remote RESTful web service, you'll probably have to make a proxy on your server and query that with a jQuery get request. See this site for an example.
常规的jQuery Ajax请求不会跨站点工作,因此,如果您想查询一个远程的RESTful web服务,您可能需要在服务器上做一个代理,并使用jQuery get请求查询。请参见本网站的示例。
If it's a SOAP web service, you may want to try the jqSOAPClient plugin.
如果是SOAP web服务,您可能想尝试jqSOAPClient插件。
#3
3
I blogged about how to consume a WCF service using jQuery:
我写了一篇关于如何使用jQuery使用WCF服务的博文:
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/
The post shows how to create a service proxy straight up in javascript.
本文展示了如何用javascript直接创建服务代理。
#4
3
Incase people have a problem like myself following Marwan Aouida's answer ... the code has a small typo. Instead of "success" it says "sucess" change the spelling and the code works fine.
万一人们像我一样对马尔文·阿维达的答案有疑问……这段代码有一个小错误。它不是说“成功”,而是说“成功”改变了拼写,代码运行良好。
#5
1
In Java, this return value fails with jQuery Ajax GET:
在Java中,jQuery Ajax GET返回值失败:
return Response.status(200).entity(pojoObj).build();
But this works:
但是这个工作原理:
ResponseBuilder rb = Response.status(200).entity(pojoObj);
return rb.header("Access-Control-Allow-Origin", "*").build();
----
Full class:
完整的类:
@Path("/password")
public class PasswordStorage {
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getRole() {
Contact pojoObj= new Contact();
pojoObj.setRole("manager");
ResponseBuilder rb = Response.status(200).entity(pojoObj);
return rb.header("Access-Control-Allow-Origin", "*").build();
//Fails jQuery: return Response.status(200).entity(pojoObj).build();
}
}
#1
29
You can make an AJAX request like any other requests:
可以像其他请求一样发出AJAX请求:
$.ajax( {
type:'Get',
url:'http://mysite.com/mywebservice',
success:function(data) {
alert(data);
}
})
#2
7
EDIT:
编辑:
The OP was not looking to use cross-domain requests, but jQuery supports JSONP as of v1.5. See jQuery.ajax(), specificically the crossDomain
parameter.
OP没有考虑使用跨域请求,但是jQuery在v1.5中支持JSONP。参见jQuery.ajax(),具体来说是跨域参数。
The regular jQuery Ajax requests will not work cross-site, so if you want to query a remote RESTful web service, you'll probably have to make a proxy on your server and query that with a jQuery get request. See this site for an example.
常规的jQuery Ajax请求不会跨站点工作,因此,如果您想查询一个远程的RESTful web服务,您可能需要在服务器上做一个代理,并使用jQuery get请求查询。请参见本网站的示例。
If it's a SOAP web service, you may want to try the jqSOAPClient plugin.
如果是SOAP web服务,您可能想尝试jqSOAPClient插件。
#3
3
I blogged about how to consume a WCF service using jQuery:
我写了一篇关于如何使用jQuery使用WCF服务的博文:
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/
The post shows how to create a service proxy straight up in javascript.
本文展示了如何用javascript直接创建服务代理。
#4
3
Incase people have a problem like myself following Marwan Aouida's answer ... the code has a small typo. Instead of "success" it says "sucess" change the spelling and the code works fine.
万一人们像我一样对马尔文·阿维达的答案有疑问……这段代码有一个小错误。它不是说“成功”,而是说“成功”改变了拼写,代码运行良好。
#5
1
In Java, this return value fails with jQuery Ajax GET:
在Java中,jQuery Ajax GET返回值失败:
return Response.status(200).entity(pojoObj).build();
But this works:
但是这个工作原理:
ResponseBuilder rb = Response.status(200).entity(pojoObj);
return rb.header("Access-Control-Allow-Origin", "*").build();
----
Full class:
完整的类:
@Path("/password")
public class PasswordStorage {
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getRole() {
Contact pojoObj= new Contact();
pojoObj.setRole("manager");
ResponseBuilder rb = Response.status(200).entity(pojoObj);
return rb.header("Access-Control-Allow-Origin", "*").build();
//Fails jQuery: return Response.status(200).entity(pojoObj).build();
}
}