【问题标题】:Posting data from Jquery Form从 Jquery 表单发布数据
【发布时间】:2014-09-16 02:09:35
【问题描述】:

我有一个简单的 jquery 表单,它从用户那里获取输入并将数据发布到另一个页面并显示成功消息。 但我的问题是数据没有发布到其他页面,而是弹出错误消息。

这是我的 Jquery:

 $(document).ready(function () {
     $("#register").click(function () {
    var name = $("#name").val();
    var email = $("#email").val();
    var password = $("#password").val();
    var cpassword = $("#cpassword").val();
    if (name == '' || email == '' || password == '' || cpassword == '') {
        alert("Please fill all fields...!!!!!!");
    } else if ((password.length) < 8) {
        alert("Password should atleast 8 character in length...!!!!!!");
    } else if (!(password).match(cpassword)) {
        alert("Your passwords don't match. Try again?");
    } else {

            $.ajax({

            type:"POST",
            url: "SignUp.aspx/register",    //trying to post to this url
            data: { firstName: name },

            success: function (msg) {
                alert("Success");        // Success message which should pop up if successful
            },
            error: alert("Failed")       //error message which should pop up if there is an error
        });
    }
});
});

这是signup.aspx页面中的代码(代码隐藏类):

         public string register(String firstName)
         {
            return "Success";            
         }

我提供的网址是正确的,没有问题。 这里可能有什么问题?

还有其他方法可以将数据发送到另一个页面吗?

感谢您的宝贵时间

【问题讨论】:

  • 开发者控制台可能会向您显示引发了哪些服务器端错误。可能是一个很好的线索。
  • @KirkWoll 未显示错误
  • 错误处理程序的语法错误,需要在函数中包装警报,您还需要防止默认表单提交。如果这些不能解决问题,则需要在控制台的 netowrk 选项卡中检查实际请求
  • @charlietfl 我试试看

标签: javascript jquery asp.net web


【解决方案1】:

这个

        success: function (msg) {
            alert("Success");        // Success message which should pop up if successful
        },
        error: alert("Failed")       //error message which should pop up if there is an error
    });

应该是:

        success: function (msg) {
            alert("Success");        // Success message which should pop up if successful
        },
        error: function(){alert("Failed")}       //error message which should pop up if there is an error
    });

但您可能会遇到其他错误。

【讨论】:

    【解决方案2】:

    您的 ajax 调用格式不正确。应该是:

    $.ajax({
            type:"POST",
            url: "SignUp.aspx/register",    //trying to post to this url
            data: { firstName: name })
            .done(function(data)
             {
                alert(JSON.stringify(data));
             })
            .success(function (msg) {
                alert("Success");        // Success message which should pop up if successful
            })
            .fail(function(err){
                alert("Failed");       //error message which should pop up if there is an error
            });
    

    【讨论】:

      【解决方案3】:

      尝试设置 ajax 请求的 contentType 选项,

      contentType:'application/x-www-urlencoded; UTF-8',
      

      希望能解决你的问题。

      【讨论】:

      • 这是 $.ajax api 中的默认值...将其设置为默认值不会做任何事情
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2014-06-11
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多