可以jQuery的AJAX调用外部web服务? [英] can jquery ajax call external webservice?

查看:222
本文介绍了可以jQuery的AJAX调用外部web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否jQuery的AJAX code从另一个域名或另一个网站调用Web服务?

像这样:

Can jquery ajax code call a webservice from another domain name or another website?
Like this:

 $.ajax({
            type: "POST",
            url: "http://AnotherWebSite.com/WebService.asmx/HelloWorld",
            data: "{'name':'" + $('#price').val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {alert(msg);}
            });



我应该怎么配置这个Web服务?


And how should i config this webservice?

推荐答案

您可以使用JSONP做跨域请求。使用jQuery你可以使用$以.json功能和像这样的URL指定的回调使JSONP请求:

you can use JSONP to make cross domain requests. with jquery you can make a jsonp request using the $.json function and specifying a callback in the url like so:

&callback=?

其实,你需要的是问号作为参数值,帕拉姆名称可以是任何东西。

Actually, all you need is the question mark as the param value, the param name can be anything.

唯一的缺点,就是你正在为必须支持JSONP请求的服务器

Only catch, is that the server you are making the request to must support jsonp

有关更深入的信息,请参阅本博客文章使JSONP工作与纽约时报的JSON API:

For more in depth information see this blog post about making jsonp work with the new york times json api:

<一个href=\"http://notetodogself.blogspot.com/2009/02/using-jquery-with-nyt-json-api.html\">http://notetodogself.blogspot.com/2009/02/using-jquery-with-nyt-json-api.html

这篇关于可以jQuery的AJAX调用外部web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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