【问题标题】:Ajax with asp.net aspx returns Undefined带有 asp.net aspx 的 Ajax 返回未定义
【发布时间】:2015-07-13 19:10:54
【问题描述】:

我面临以下问题:每当我点击button 时,alert 会显示undefined

网络方式:

    [WebMethod]
    public static string getTest()
    {
        return "testing success";
    }

Ajax 脚本

    <script type="text/javascript">
        function getTest() {
            $.ajax({
                type: "POST",
                url: "main.aspx/getTest",
                data: "{}",
                datatype: "json",
                contenttype: "/application/json; charset=utf-8",
                success: function (msg) {

                    alert(msg.d);

                },
                error: function (data) {
                }
            });
        }
    </script>

【问题讨论】:

  • contenttype: "/application/json; charset=utf-8" 从应用程序的开头删除 /。

标签: asp.net ajax json undefined


【解决方案1】:

datatype 应该是 dataTypecontenttype 应该是 contentType。同时从"/application/json; charset=utf-8"的开头删除/

$.ajax({
            type: "POST",
            url: "main.aspx/getTest",
            data: "{}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {

                alert(msg.d);

            },
            error: function (data) {
            }
        });

【讨论】:

  • Man u 拯救了我的一天...为此花费了数小时,非常感谢。
【解决方案2】:

从你的 ajax 调用中删除这些

datatype: "json",
contenttype: "/application/json; charset=utf-8",

更多关于这些

Differences between contentType and dataType in jQuery ajax function

What is content-type and datatype in an AJAX request?

您可以在jQuery Ajax documentation找到更多详细信息

【讨论】:

  • 我做了,但仍然未定义。
  • 试试 alert alert(msg); 看看你得到了什么?
猜你喜欢
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
相关资源
最近更新 更多