【问题标题】:ajax call not working in external js fileajax调用在外部js文件中不起作用
【发布时间】:2014-02-20 12:27:16
【问题描述】:

我有一个只有用户名和密码字段的注册页面。我想在提交按钮的单击事件上提交新的用户详细信息。请告诉我如何使用 jquery 使用 ajax 调用。我在 ASP.net 中定义了单击按钮时的插入功能。但它不起作用。

function Insert() {
    alert("Insert");

    var uname = $('#txtRUserName').val();

    alert(uname)
    var password = $('#txtRPassword').val();
    $.ajax({type: "POST",
            url: "login.aspx/Insert",
            data: "{'uname:'" + uname + ",'password:'" + password + "}",
            datatype: "text/html",
            cache: false,
            async: false,
            contentType: "text/html",
            success: function processData(r) {
                var myItem = r.d.split('#');
                alert(myItem)
            }
            //error: errorAlert
        });
    alert("jqajax")
}

【问题讨论】:

  • 哪部分不工作,你的错误有没有解决。你期待发生什么?该表单是否仍用于发布或仅运行该功能

标签: javascript c# jquery asp.net ajax


【解决方案1】:

有效的 json、ContentType 和 DataType

您可以将调用更改为有效的 json,即

data: "{uname:'" + uname + "',password:'" + password + "'}",

您的内容类型必须是:

contentType: "application/json; charset=utf-8"

您的数据类型必须是:

dataType: "json"

所以你的 javascript 会变成:

function Insert() {
    alert("Insert");

    var uname = $('#txtRUserName').val();

    alert(uname)
    var password = $('#txtRPassword').val();
    $.ajax({type: "POST",
            url: "login.aspx/Insert",
            data: "{uname:'" + uname + "',password:'" + password + "'}",
            datatype: "json",
            cache: false,
            async: false,
            contentType: "application/json; charset=utf-8",
            success: function processData(r) {
                var myItem = r.d.split('#');
                alert(myItem)
            }
            //error: errorAlert
        });
    alert("jqajax")
}

启用页面方法

您需要在脚本管理器中启用页面方法,如下所示:

<asp:ScriptManager runat="server" EnablePageMethods="true">

代码后面的页面

您还需要确保您的方法具有[WebMethod] 属性并且也是静态的,例如:

[WebMethod]
public static bool Insert(string uname, string password)

【讨论】:

  • jQuery 会自动执行此操作。
  • 实际上撤回了..我刚刚看到数据由于某种原因被用引号括起来。
  • @MichaelCoxon 是的,当然。
  • 发送的数据不是 JSON,即使看起来很像。键被错误的字符引用,值根本没有被引用。
  • @MichaelCoxon 如果您只想快速实现一个只在一个地方使用的 ajax 函数,而不必为它敲开一个 web 服务,那么在 web 表单中非常方便。 :)
【解决方案2】:

在发布表单时,$(this).serializeArray(); 会处理输入数据

  $(document).ready(function(){
        $('form').submit(function( e ) {
            var postData = $(this).serializeArray();
            $.ajax({
               datatype: "text/html",
               cache: false,
               async: false,
               contentType: "text/html",
                type: 'post',
                url: 'page.php',
                data: postData,
                success: function () {
                  var myItem = r.d.split('#');
                  alert(myItem)
                }
            });
            e.preventDefault();
        });
    });  

编辑: 将 $('form') 更改为表单 id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多