【问题标题】:calling a webservice with jquery ajax jsonp :error使用 jquery ajax jsonp 调用 web 服务:错误
【发布时间】:2015-01-03 05:23:39
【问题描述】:

我正在尝试使用 ajax 调用 web 服务,我得到一个内部 500 错误。请你告诉我不确定我做错了什么,我可以毫无问题地调用 webmethod。

JQUERY AJAX CALL

<script type="text/javascript">

function LoginVailid() {

    $.ajax({
        url: "http://localhost:49301/AppService.asmx/LoggonAuthentication",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "jsonp",
        jsonp: "callback",
        crossDomain: true,
        success: function (json) {
            alert(json.d);
        },
        error: function () {
            alert("Hit error fn!");
        }
    });
}
</script> 

网络服务方法

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

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

}

【问题讨论】:

  • 你能不能把dataTypejsonp改成json
  • 如果我这样做,则会得到以下 ERROR.XMLHttpRequest cannot load localhost:49301/AppService.asmx/LoggonAuthentication?{}。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'localhost:55255' 不允许访问。响应的 HTTP 状态代码为 500。
  • > 我正在尝试使用 ajax 调用 web 服务,我得到一个内部 500 错误。你能调试和检查真正的异常吗?
  • learn.jquery.com/ajax/working-with-jsonp JSONP 的出现——本质上是一种自愿的跨站点脚本攻击——为强大的内容混搭打开了大门。许多知名网站都提供 JSONP 服务,允许您通过预定义的 API 访问其内容。
  • 查看SO question

标签: c# jquery ajax json jsonp


【解决方案1】:

试试这个代码

[WebMethod]
[ScriptMethod(UseHttpGet = true, XmlSerializeString=false, ResponseFormat = ResponseFormat.Json)]
public string LoggonAuthentication(string callback)
{
    return  callback + "({message: 'Hello World'})";
}

articlethis SO question 也可以提供帮助

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 2013-05-10
    • 2014-06-22
    • 1970-01-01
    • 2012-02-11
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多