jquery ajax 可以调用外部网络服务吗? [英] can jquery ajax call external webservice?

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

问题描述

jquery ajax 代码可以从另一个域名或另一个网站调用 webservice 吗?
像这样:

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); }
});

我应该如何配置这个网络服务?

推荐答案

您可以使用 JSONP 进行跨域请求.使用 jquery,您可以使用 $.json 函数发出 jsonp 请求并在 url 中指定回调,如下所示:

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:

http://notetodogself.blogspot.com/2009/02/using-jquery-with-nyt-json-api.html

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

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