ASP.NET AJAX 到函数调用后面的代码错误

发布于 2024-12-22 02:11:03 字数 1021 浏览 3 评论 0原文

我正在使用以下调用将数据发送回服务器:

$.ajax({
  type: "POST",
  url: "MyTestPage.aspx/UpdateData",
  data: updates,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (msg) {
                 alert('Success!');
  },
  error: function (msg) {
                  alert('Failure!');
  }
});

后面的代码看起来像

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace TestNamespace
{
    public partial class MyTestPage: System.Web.UI.Page
    {
        [WebMethod]
        public static bool UpdateData(string IDs, string Values)
        {
            return true;
        }

    }
}

来自 Chrome 的请求负载,即 UI 更新变量的值是: { IDS : "21TOK31" , VALUES : "2TOK2"}

请求没有命中函数后面的代码,并且 ajax 函数总是因为错误而进入失败块。

当没有传递数据并且代码隐藏函数没有参数时,请求会命中代码隐藏函数。

我是在 ASP.NET 中使用 JSON 的新手。有人可以指导我可能出现的问题吗?以及如何最好地解决这个问题。

I am using the following call to send data back to the server:

$.ajax({
  type: "POST",
  url: "MyTestPage.aspx/UpdateData",
  data: updates,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (msg) {
                 alert('Success!');
  },
  error: function (msg) {
                  alert('Failure!');
  }
});

The code behind looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace TestNamespace
{
    public partial class MyTestPage: System.Web.UI.Page
    {
        [WebMethod]
        public static bool UpdateData(string IDs, string Values)
        {
            return true;
        }

    }
}

From Chrome the request pay load i.e. value of the UI updates variable is: { IDS : "21TOK31" , VALUES : "2TOK2"}

The request does not hit the code behind function and the ajax function always goes to the failure block because of the error.

The request hits the code behind function when no data is being passed and the code behind function has no parameters.

I am new to using JSON in ASP.NET. Can someone please guide me to what the problem might be? And how best it might be solved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-12-29 02:11:03

将错误函数修改为如下所示:

error: function (xhr, err) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); }

您将看到确切的错误。

就我个人而言,我只是在给我带来问题的函数中放置断点,然后在本地运行它来调试,

modify the error function to be like this:

error: function (xhr, err) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); }

and you will see the exact error.

personally i just put breakpoints inside the function giving me problems and run it locally to debug,

韬韬不绝 2024-12-29 02:11:03

要进行问题排查:

  1. 打开 Chrome 调试器
  2. 点击在“网络”选项卡上
  3. 像您一直在做的那样提交 ajax 请求
  4. 您应该看到对 MyTestPage.aspx/UpdateData 的请求 - 单击它
  5. 在“预览”选项卡下,您将看到 .NET 返回的错误消息

如果如果您仍然遇到问题,请发布结果,这将为您提供正在发生的真正错误。

To troubleshoot:

  1. Open the Chrome debugger
  2. Click on the "Network" tab
  3. Submit the ajax request as you've been doing
  4. You should see the request to MyTestPage.aspx/UpdateData - click on it
  5. Under the "Preview" tab, you'll see the error message that .NET is returning

If you're still having problems, post the result of that, which will give you the real error that's happening.

漫雪独思 2024-12-29 02:11:03

如果您使用以下内容,它是否有效:

var updates = '{"IDs":"21TOK31","Values":"2TOK2"}';

webconfig 文件中还存在需要设置的配置设置,但我假设您由于空返回值而具有这些配置设置。

您可能需要的另一件事是转换器,因为 asp 返回数据的格式:(在您的 javascript ajax 调用中)

converters: {
    "json jsond": function(msg)
    {
        return msg.hasOwnProperty('d') ? msg.d : msg;
    }
},

并更改:

contentType: "application/json",

If you use the following, does it work:

var updates = '{"IDs":"21TOK31","Values":"2TOK2"}';

There are also configuration settings in the webconfig file that need set, but I make the assumption you have those due to your empty return value working.

One other thing you might need is a converter due to the format that asp returns data:(in your javascript ajax call)

converters: {
    "json jsond": function(msg)
    {
        return msg.hasOwnProperty('d') ? msg.d : msg;
    }
},

and change:

contentType: "application/json",
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文