如何在ajax中调用跨域asmx Web服务。如何允许从ajax中的其他域调用Web服务 [英] how to call a cross domain asmx web service in ajax . how to allow a web service to be callable from other domain in ajax

查看:88
本文介绍了如何在ajax中调用跨域asmx Web服务。如何允许从ajax中的其他域调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友......我正在尝试从这么多天来从javascript调用一个web服务...这在另一个域上可用...意味着跨域Web服务。我一次又一次地失败,任何人都可以告诉我如何以富有成效的方式做到这一点。



1:服务是这样的..



[WebService(Namespace =" http: //tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

//允许从脚本调用此Web服务,使用ASP.NET AJAX,取消注释以下行。

[System.Web.Script.Services.ScriptService]

公共类阵营:System.Web.Services.WebService {< br $>


公共营地(){



//如果使用设计组件,请取消注释以下行

// InitializeComponent();

}



[WebMethod,ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true) ]

public string HelloWorld(){

return" Hello World ... it cross domain";

}



}









2:和脚本功能是这样的....





< script type =text / javascript>





$(文件).ready(function(){



var surl =http:// localhost :1061 / camp_web_service / camp.asmx / HelloWorldcallback = hello;

$ .ajax({

类型:'GET',

url :surl,

crossDomain:true,

contentType:application / json; charset = utf-8,



dataType:jsonp,

成功:函数(msg){

$ .each(msg,function(name,value){

alert(value);

});

},

错误:函数(xhr,状态,错误){alert('Servidor de error 404 !!');},

async:true,

cache:false

});

});

< / script>

hello friends... i am trying from so many days to call a web service from javascript... which is available on another domain... means cross domain web service . and i am failing again and again , can anyone tell me how to do it in productive way.

1: the service is like this ..

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class camp : System.Web.Services.WebService {

public camp () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public string HelloWorld() {
return "Hello World... its cross domain";
}

}




2: and script function is like this ....


<script type="text/javascript">


$(document).ready(function () {

var surl = "http://localhost:1061/camp_web_service/camp.asmx/HelloWorldcallback=hello";
$.ajax({
type: 'GET',
url: surl,
crossDomain: true,
contentType: "application/json; charset=utf-8",

dataType: "jsonp",
success: function (msg) {
$.each(msg, function (name, value) {
alert(value);
});
},
error: function (xhr, status, error) { alert('Servidor de error 404 !!'); },
async: true,
cache: false
});
});
</script>

推荐答案

(document).ready(function(){



var surl =http:// localhost:1061 / camp_web_service / camp.asmx / HelloWorldcallback = hello;
(document).ready(function () {

var surl = "http://localhost:1061/camp_web_service/camp.asmx/HelloWorldcallback=hello";


.ajax({

类型:'GET',

url:surl ,

crossDomain:true,

con tentType:application / json; charset = utf-8,



dataType:jsonp,

成功:函数(msg){
.ajax({
type: 'GET',
url: surl,
crossDomain: true,
contentType: "application/json; charset=utf-8",

dataType: "jsonp",
success: function (msg) {


.each(msg,function(name,value){

alert(value);

});

},

错误:函数(xhr,状态,错误){alert('Servidor de error 404 !!');},

async:true,

缓存:false

});

});

< / script>
.each(msg, function (name, value) {
alert(value);
});
},
error: function (xhr, status, error) { alert('Servidor de error 404 !!'); },
async: true,
cache: false
});
});
</script>


这篇关于如何在ajax中调用跨域asmx Web服务。如何允许从ajax中的其他域调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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