如何在ajax中调用跨域asmx Web服务。如何允许从ajax中的其他域调用Web服务

编程入门 行业动态 更新时间:2024-10-27 02:25:14
本文介绍了如何在ajax中调用跨域asmx Web服务。如何允许从ajax中的其他域调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好朋友......我正在尝试从这么多天来从javascript调用一个web服务...这在另一个域上可用...意味着跨域Web服务。我一次又一次地失败,任何人都可以告诉我如何以富有成效的方式做到这一点。 1:服务是这样的.. [WebService(Namespace =" http: //tempuri/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //允许从脚本调用此Web服务,使用ASP.NET AJAX,取消注释以下行。 [System.Web.Script.Services.ScriptService] 公共类阵营:System.Web.Services.WebService { 公共营地(){ //如果使用设计组件,请取消注释以下行 // 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 = "tempuri/")] [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 = "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 = "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服务

本文发布于:2023-10-07 10:49:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   ajax   Web   asmx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!