·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ajax跨域访问 webservice

ajax跨域访问 webservice

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

Ajax跨域访问 webservice

前端代码

    $.ajax({            type: "POST",            url: "http://localhost:9767/WebService1.asmx/HelloWorld?jsoncallback=?",            data: "{}",            dataType: "jsonp",            success: function (data) {                                alert(data.result);            }        });

webservice 的代码

  public void HelloWorld()        {            string callbackMethodName = HttpContext.Current.Request.Params["jsoncallback"] ?? "";            HttpContext.Current.Response.Write(callbackMethodName + "({result:'Hello World'})");            HttpContext.Current.Response.End();                  }

 这样会得到

 但是如果用这样的代码

如果url不加?jsoncallback=?

  $(function () {        $.ajax({            type: "POST",            url: "http://localhost:9767/WebService1.asmx/HelloWorld",            data: "{}",            dataType: "jsonp",            success: function (data) {                alert(data.result);            }        });

  

  public string HelloWorld()        {            return "Hello World";        }

  

则会返回一个xml (

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">Hello World</string>

) 并且会不去回调函数 这是怎么回事,笔者对webservice 不是很了解 也是刚刚接触 有知道的能告诉下笔者 笔者会万分感谢