如何使用JavaScript从跨域调用Web Service

问题描述:

我在通过JavaScript从跨域调用Web服务时遇到问题



我的服务托管在http:/abc.com/mywebservice.asmx/helloword

如果我从http:/abc.com/default.html调用我的网络服务,那么它正在工作

但如果我打电话给我的网络服务表格另一个域名如httpp:/bcd.com/form.html然后它不起作用并给我错误

我的ajax调用: -

I have a Problem on calling web service From Cross Domain By JavaScript

My Service is hosted at "http:/abc.com/mywebservice.asmx/helloword"
and if i call my web service from from "http:/abc.com/default.html " then it is working
but if i call my web service form another domain like httpp:/bcd.com/form.html then it is not working and giving me error
my ajax call:-

$.ajax({
        type: "POST",
        url: "http:/abc.com/mywebservice.asmx/helloword",
        data: {},
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            Game._onSessionGot(response.d);
        }
    });

请帮帮我

.ajax({
type: POST
url: http:/abc.com/mywebservice.asmx/helloword
data:{},
cache: false
contentType: application / json; charset = utf- 8
dataType: json
成功: function (response){
Game._onSessionGot(response.d);
}
});
.ajax({ type: "POST", url: "http:/abc.com/mywebservice.asmx/helloword", data: {}, cache: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { Game._onSessionGot(response.d); } });

请帮帮我


正如这里提到的 [ ^ ],对于跨域Web服务,您可以使用JSONP。

参考: JSON和JSONP [ ^ ]

示例实现:跨域JSONP与jQuery调用循序渐进指南 [ ^ ]



总的来说,建议将请求地址放在脚本标签中,然后在需要时解析内容。





另一篇相同的详细文章:呼叫Cross Do AJAX中的主要Web服务 [ ^ ]
As mentioned here[^], for cross domain webservice you can use JSONP.
Refer: JSON and JSONP[^]
Example implementation: Cross-domain JSONP with jQuery call step-by-step guide [^]

Overall, it suggests, to place the requesting address in the script tag, and then parse the content when needed.


Another detailed article on the same: Calling Cross Domain Web Services in AJAX[^]