首页 > 学院 > 开发设计 > 正文

ajax跨域访问 webservice

2019-11-17 02:06:47
字体:
来源:转载
供稿:网友

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 不是很了解 也是刚刚接触 有知道的能告诉下笔者 笔者会万分感谢


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表