【问题标题】:$.ajax ReferenceError: data is not defined$.ajax ReferenceError: 数据未定义
【发布时间】:2014-04-15 19:42:52
【问题描述】:

我来了

ReferenceError: 数据未定义 和

>NetworkError: 500 Internal Server Error - http://localhost/WebApplication2/js/test.aspx"

但是我的 aspx 页面在正确的目录中

我做了所有这些来消除内部服务器错误:

http://pcsupport.about.com/od/findbyerrormessage/a/500servererror.htm

代码按照教程

Index.html(回调数据会放在这里)

<body>
    <input id="button" type="submit" value="Go" />
    <div id="feedback">    </div>


</body>


这是我的 Ajax 方法:

$('#button').click(function () {
    $.ajax({
        type: "POST",
        url: "test.aspx",
        data: {"name=" + name},
        success: function (data) {
            $("#feedback").html(data);
        }
    });
});

test.aspx

<form id="form1" runat="server">
<div>
<%Response.Write(Request.Form["name"].ToString()); %>
</div>
</form>

类似的帖子:

ajax and “Uncaught ReferenceError: data is not defined”
$.ajax() and "Uncaught ReferenceError: data is not defined"
Ajax data - Uncaught ReferenceError: date is not defined

编辑:JSLint /JSHint /SharpLinter 对于检测语法错误仍然没有多大帮助。我需要一些语法检查建议

也存在放置数据类型参数的问题.. 这不是我的情况。

我在那里找不到简单的东西..请建议

【问题讨论】:

  • 我在给定的代码中看不到任何可能导致该错误的内容。
  • 发布 xhr 响应的原始文本。您可以使用 fiddler 或 chrome 开发工具来获得它。
  • 好的。我在使用 chrome 开发工具 .. 你能告诉我确切的选择吗?
  • 在控制台我收到此错误:NetworkError: 500 Internal Server Error - localhost/WebApplication2/js/test.aspx" ..但我的页面在正确的目录中
  • 500 不是 404。您的 test.aspx 抛出错误。

标签: javascript jquery html ajax


【解决方案1】:

我猜,您应该将数据传递为:

data: {name: name},

另外,请尝试在成功函数中使用其他变量,而不是 data

success: function (response) {
   $("#feedback").html(response);
}

【讨论】:

  • 显然没有任何意义
  • 我不是故意粗鲁的......我通常会在发布之前做一些小作业/更改变量/使用空数据/编码数据等进行测试......我的意思是说它不会改变任何东西......包括错误
【解决方案2】:

好的..终于让它工作了...错误在数据参数中(在url之后)

而不是(这是教程代码)

data: {"name=" + name1},

这对我有用

data : {'name':name1},

这是工作代码:

脚本

$('#button').click(function () {
var name1="aa";
    $.ajax({
        type: "POST",
        url: "test.aspx",
        data : {'name':name1},
        statusCode: {
            404: function () { $("#messege").text("Page not found"); },
            500: function () { $("#messege").text("internal server error"); }
        },
        success: function (data) {
            $("#messege").html(data); 
        } });
});

其余部分相同。以下链接相关且有用

jquery POST data in aspx page

【讨论】:

    【解决方案3】:

    在我的例子中,我在 Bootstrap Modal 表单中加载响应,但它没有在视图中声明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多