【问题标题】:jsonp with asmx error 500带有asmx错误500的jsonp
【发布时间】:2014-07-20 23:12:24
【问题描述】:

当我尝试这个时,它确实有效

 function test() {
    debugger;
    $.ajax({ url: "http://testweb.com/myAPI.asmx/GetPersonTest",
    type: "GET",
    contentType: "application/json; charset=utf-8",
    data: { userid: 1 },
    dataType: "jsonp",
    success: function(json) {
        alert(json.UserID + ' ' + json.FirstName + ' ' + json.LastName);
    },
    error: function() {
        alert("Hit error fn!");
    }
    });
}

在同一个 asmx 中,我有另一种方法,如下所示

    function Post_test(){

var newURL = window.location.protocol + '//' + window.location.host + '/' + window.location.pathname;
var user = document.getElementById('Username').value;
var email = document.getElementById('email').value;
debugger;
$.ajax({ url: "http://testweb.com/myAPI.asmx/GetPerson",
    type: "GET",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ username: user, emailID: email}),
    dataType: "jsonp",
    success: function(json) {
        alert(json.UserID + ' ' + json.FirstName + ' ' + json.LastName);
    },
    error: function(json) {
        alert("Hit error fn!");
    }
});


}

此方法插入数据库并返回与 GetPersonTest 相同的结果。但是当我通过 javascript 和 jsonp 调用它时,它会给出 500 服务器错误

当我签入调试器时,它给出了以下错误

jQuery17106155389654450119_1401518867750({"Message":"Invalid web service call, missing value for parameter: \u0027username\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"});

任何人都可以在下面帮助我。谢谢

方法的头部是

         <WebMethod(True)> _
    <ScriptMethod(UseHttpGet:=True, ResponseFormat:=WebMessageFormat.Json)> _
        Public Function GetPerson(ByVal username As String, ByVal emailID As String) As String

End Function

当我在开发人员工具中检查标题时,它在下面

callback:jQuery17106155389654450119_1401518867750
{"username":"testuser","emailID":"test@test.com"}:
_:1401518889467
Response Headersview source

【问题讨论】:

    标签: javascript asp.net web-services jsonp


    【解决方案1】:

    请检查参数 username 是否在上述合约方法中使用。因为,您的错误显示为缺少 username 参数。

    确保这一点并尽快发布您的回复。

    【讨论】:

    • 我已经在问题中添加了方法细节。
    • 我刚刚检查了chrome rest客户端,它说System.InvalidOperationException:缺少参数:用户名。在 System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 不知道我错过了什么因为我提供了用户名
    【解决方案2】:

    您可以尝试以下更改:

    1. 从数据参数中删除'JSON.stringify',所以它应该看起来像

      数据:{“用户名”:用户,“emailID”:电子邮件}

    2. 响应格式为“WebMessageFormat.Json”,所以将dataType改为“json”,代替“jsonp”。

    3. 由于是作为查询字符串而不是 JSON 发送的,您可以放心地省略“contentType”(或者可以只删除“应用程序/json”)。

    希望这会有所帮助。

    【讨论】:

    • json 不能跨域工作。我需要它在跨域环境中工作。这就是我使用 jsonp 的原因
    • 如果您可以访问服务器端代码并​​且可以修改响应标头,请设置正确的“Access-Control-Allow-Origin”标头。在这种情况下,跨域 JSON 请求也将起作用。有关 CORS 请求,请参考 [link]en.wikipedia.org/wiki/Cross-origin_resource_sharing
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多