【问题标题】:Jquery $ajax funtion returns 500 Internal Server Error in C#Jquery $ajax 函数在 C# 中返回 500 内部服务器错误
【发布时间】:2017-10-25 13:18:22
【问题描述】:

单击按钮时,我在页面加载后将一些数据传递给 .cs 文件。但是在调用 ajax 函数时出现 500 Internal Server Error

Ajax 函数,

              $.ajax({
                type: "POST",
                url: "Home.aspx/getSelectedData",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                //async: true,

调用函数,

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<pageResult> getSelectedData(string search_value)
{}

我收到以下错误,

 **POST http://localhost:4519/Home.aspx/getSelectedData 500 (Internal Server Error)**

【问题讨论】:

  • 服务器代码/背后代码错误 - 在服务器环境中打开人类可读的错误代码。
  • 这可能有问题: type: "POST" [ScriptMethod(UseHttpGet = true)]
  • 我已经尝试过没有这个但那些都不起作用
  • 你传递什么样的数据?
  • 我正在传递字符串值

标签: javascript c# jquery ajax asp.net-ajax


【解决方案1】:

像这样使用 .cs 文件,

[WebMethod]
public static List<pageResult> getSelectedData(string search_value)
{}

对于在 aspx 中的 ajax 调用,您应该将方法定义为 Static 然后只有它才能为您工作。

【讨论】:

    【解决方案2】:

    contentType 是您发送的数据类型,所以application/json;

    默认为application/x-www-form-urlencoded; charset=UTF-8

    如果你使用application/json,你必须使用JSON.stringify()来发送JSON对象。

    JSON.stringify() 将 javascript 对象转换为 json 文本并将其存储在字符串中。

    $.ajax({
          type: "POST",
          url: "Home.aspx/getSelectedData",
          data: JSON.stringify(data),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
    

    【讨论】:

      【解决方案3】:

      将您的 ajax 类型更改为 get

      $.ajax({
                      type: "get",
                      url: "Home.aspx/getSelectedData",
                      data: data,
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-16
      • 2020-06-25
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2019-11-12
      • 2019-12-10
      • 2013-12-07
      相关资源
      最近更新 更多