【问题标题】:Ajax in asp page return 500 [closed]asp页面中的Ajax返回500 [关闭]
【发布时间】:2018-09-06 07:36:55
【问题描述】:

我正在尝试执行 ajax POST,但总是返回 500 服务器错误。

为了进行测试,我简化了请求。

Javascript 代码:

 $(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
      $("#prova").click(function() {
        $.ajax({
          type: "POST",
          url: "Solicitud.aspx/GetDate",
          data: {someParameter: "some value"},
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            // Replace the div's content with the page method's return.
            console.log("Resposta"+ msg.d);
          }
        });
      });
        });

类代码

namespace picovirgiliop
{
    public partial class Solicitud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetDate(string someParameter)
        {
            return DateTime.Now.ToString();
        }
...

编辑

POST http://localhost:63010/Solicitud.aspx/GetDate 500(内部服务器错误)

  • 发送@jquery-3.3.1.js:9600
  • ajax@jquery-3.3.1.js:9206
  • (匿名)@ Solicitud:300
  • 调度@jquery-3.3.1.js:5183
  • elemData.handle @ jquery-3.3.1.js:4991

【问题讨论】:

  • 响应正文是否为您提供了有关错误的线索?
  • 您能否发布响应正文的屏幕截图
  • 如果我转到 localhost/Solicitud.aspx/GetDate 页面,它打开了 solicitud 页面并返回 200。不要返回 500 错误的更多信息,我在编辑部分复制消息。
  • 删除contentType: "application/json; charset=utf-8", 选项(您没有对数据进行字符串化)
  • 如果我删除 contenttype,不会返回 500 错误,但也不会返回成功控制台日志

标签: c# jquery asp.net ajax


【解决方案1】:

根据您的代码审查,我可以看到问题

数据:{someParameter:“某个值”},

应该像下面这样,

data: "{ 'someParameter': 'some value' }",

data: "{ someParameter: 'some value' }",

【讨论】:

    【解决方案2】:

    您需要对传递给服务器的数据进行字符串化。将此代码 data: {someParameter: "some value"} 替换为以下代码行:

     data: JSON.stringify({"someParameter": "some value"}),
    

    【讨论】:

    • 问题出在 WebMethod 上。如果我删除字符串 someParameter 输入参数,则该方法正确调用
    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 2021-05-12
    • 2016-12-15
    • 2014-09-13
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多