【问题标题】:using Get for ajax calls gives me 500 error使用 Get 进行 ajax 调用给我 500 错误
【发布时间】:2014-10-27 12:10:12
【问题描述】:

我正在使用 POST 调用 [web 方法],但当我“获取”数据时,我正尝试使用 GET。

使用 Post 作品。 使用 GET 给我一个 500 错误。

这是对我的 [web 方法] 的主要 jquery 调用:

        $.ajax({
            type: 'GET',
            contentType: 'application/json',
            dataType: 'json',
            url: 'Cloud/Feed.aspx/GetNextFrames2',
            data: '{ test: "hime"}',

~

这是我的测试[网络方法]。

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetNextFrames2(string test)
{
    return 'test'
}

如果我不传递任何参数,使用 GET 将不会出错。 只要添加一个参数,我就会得到 500 内部错误。

我用过wireshark和Fiddler,但看不到任何有用的东西。

这显然取决于使用参数。所以,至少我已经查明了错误在哪里。

我已经尝试将参数直接附加到url:

myurl?par=testme...

但还是同样的错误。

我还能尝试什么?

谢谢

【问题讨论】:

  • 500 错误不是 ajax 脚本中的问题,而是 url 目标中的问题。如果你使用 php 我可以帮助解决问题:)
  • 将网址更改为完整的网址,例如servername/Cloud/Feed.aspx/GetNextFrames2
  • @kefy 如果只有我是!如果我使用 php,如果谷歌结果如何诊断问题,我发现很多,但唉 asp.net :(
  • @SainPradeep 您好,感谢您的建议。我以前试过这个,但还是不高兴:(

标签: jquery asp.net ajax get webmethod


【解决方案1】:

上面关于contentType GET 的cmets 是正确的但是 WebMethods 需要它 - see this detailed post on why.

真的,除非您使用POST,恕我直言,让事情与WebMethods 一起工作只是一种痛苦。 如果方法有参数(它会爆炸),我什至不能让它与GET一起工作,所以如果有人能让它工作,太棒了!请注意,即使使用 GET 的链接示例也包含一个参数...不确定这是否只是 使用 为真的(该文章上的日期是 2007)。 s>

所以如果你必须(根据上面的答案更新):

    $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "Cloud/Feed.aspx/GetNextFrames2",
            //data: {"test": "foo"}, // GET -> Cloud/Feed.aspx/GetNextFrames2?test=foo
            data: {"test": "'foo'"}, //value now in quotes
            ...

网络方法:

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetNextFrames2(string test)
    {
        //Should now work based on answer above

        //Yes, this is FUBAR, but technically feasible (no need for params in method)
        //var query = HttpContext.Current.Request.QueryString;

        //now do what you need to do with your querystring data 
        // re: that's where your data is in GET ($.get)

        ....           
    }

其他令人敬畏的笔记:

  • 如果你有FriendlyUrls,你必须注释掉 AutoRedirectMode = RedirectMode.Permanent in RouteConfig -> FriendlyUrlSettings - 因为它按照它所说的做并将.aspx 重定向到一个“友好的网址”...是的,包括上面...

希望这会有所帮助,也许会让你想到(而不是)WebAPI :)


更新摘要

好吧,如上所示的答案是将string 括在引号中 - 所以它似乎(强烈)输入参数。所以如果int:

  • 如果 $.get 数据:{ "test": 55 }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetNextFrames2(int test)
    {
      ...
    

【讨论】:

  • 您好,感谢您提供的额外信息和链接。你给了我关于事情和调查的东西。总是一件好事:)
  • @AndrewSimpson 对我有帮助(WebMethods),所以这对我来说也是一个重新学习的过程 :)
  • 您好,感谢您的总结。是的,我知道我通常使用 Web API,我一直想知道为什么我必须包含 GET 和 POST 属性(服务器端)才能从 JS 发出 GET 请求。现在,我想我知道了..
【解决方案2】:

Ajax GET 请求不应具有 Content-type,因为它们没有任何实体主体。

供参考:Do I need a content type for http get requests?

试试

$.ajax({
        type: 'GET',
        dataType: 'JSON',
        url: "Cloud/Feed.aspx/GetNextFrames2?test='hime'",

其中任何一个都应该工作

【讨论】:

  • 你好,在 mo 忙着工作。感谢您的建议,我已经尝试过了。我仍然遇到同样的错误...
  • GET 方法没有内容。通过 GET 发送的任何数据都必须在 url 中发送
  • @Péter 这很有意义,这也是我试图理解的。我接受的答案部分基于您的评论。谢谢!
【解决方案3】:

问题在于查询字符串中的值。该值应在报价范围内。下面的代码正在运行。

$.ajax({
            type: 'GET',
            contentType:'application/json',
            dataType: 'json',
            cache:false,
            url: "TestWebMathod.aspx/GetNextFrames2?test='hime'",
            error: function (error) {
                alert(error.responseText)
            },
            success: function (result) {
                alert(result.d)

            }

        });

还要查看 error.responseText 以了解确切的异常消息。这将有助于解决错误。

【讨论】:

  • 嗨,是的,它确实有效。太感谢了。我需要在 url 中包含 's。高超!我很高兴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多