简体   繁体   English

使用带有参数的JQuery调用asmx Web服务会导致500错误

[英]Calling asmx web service using JQuery with parameters causes 500 error

I have a simple enough web service: 我有一个足够简单的Web服务:

        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public string GetToken(string a)
        {
        }

And I'm calling it client side using JQuery: 我使用JQuery将其称为客户端:

    $.ajax({
        post: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        url: '../url/GetToken',
        data: "{'a':'test'}",
        success: function (data) {
        },
        error: function (a, b, c) {
        }
    });

The call always fails and the error returned is 500 Internal Server Error. 调用总是失败,返回的错误是500内部服务器错误。 I have placed a breakpoint inside the web service, and the code isn't being reached at all. 我在Web服务中放置了一个断点,根本没有访问代码。 When I modify the web service to take no arguments at all (and remove the data element from the JQuery call) the call succeeds. 当我修改Web服务以完全不参数(并从JQuery调用中删除data元素)时,调用成功。 I have played around with different ways to pass the data element; 我用不同的方式来传递data元素; I've passed a JSON object (no quotes), and I've removed the quotes from around the a argument. 我通过一个JSON对象(不包括引号),和我从周围的去除引号a说法。 None of it works. 它都不起作用。

Edit: 编辑:

Using Fiddler I have determined that the actual error causing the 500 is "Invalid web service call, missing value for parameter". 使用Fiddler我已经确定导致500的实际错误是“无效的Web服务调用,缺少参数值”。

Edit 2: 编辑2:

Passing data this way works: 以这种方式传递数据有效:

data: "a='test'"

I have no idea why. 我不知道为什么。 Any ideas? 有任何想法吗?

Haven't tested this, but in your code snippet you have post: 'GET' rather than type: 'GET' . 没有测试过这个,但是在你的代码片段中你有post: 'GET'而不是type: 'GET' That may be causing the server to reject the contentType as json (which is why your data isn't being serialized properly). 这可能导致服务器拒绝将contentType作为json(这就是为什么您的数据没有被正确序列化)。

You could use a tool like Fidler to check the response you are getting back. 您可以使用Fidler之类的工具来检查您要回复的响应。 Most likely there is an error page being returned, but since it is a jquery ajax call you are not seeing it. 最有可能返回一个错误页面,但是由于它是一个jquery ajax调用,因此您看不到它。

I couldn't use the GET, had to use POST as type, and my web service is in the same directory, but this worked: 我无法使用GET,不得不使用POST作为类型,我的Web服务在同一目录中,但这有效:

    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        url: 'url.asmx/GetToken',
        data: "{'a':'test'}",
        success: function(data) {
            alert("Success");
        },
        error: function(a) {
            alert(a.responseText);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM