jqueryajax帖子将我发送到操作页面,并且不会显示结果数据

jquery ajax post sends me to action page and wont show result data

本文关键字:数据 结果 显示 操作 jqueryajax      更新时间:2023-12-09

我有以下脚本用于发出Ajax/Jquery post请求。脚本有效(我在后端得到了正确的响应)。但我似乎无法发出任何警报,所以我认为成功函数存在一些问题。你们看到明显的错误了吗?浏览器会得到正确的响应(检查chrome中的网页)。

<script>
        $(document).ready(function() {
            var frm = $('#registerform');
                frm.submit(function(x) {
                    x.preventDefault();
                    $.ajax({
                        type: 'POST',
                        url: 'http://localhost:8080/api/v1/register',
                        data: frm.serialize(),
                        crossDomain: true,
                        success: function(data){
                            if(data == 200){
                                alert("successfully registered");
                                $('#alert').append("successfully registered");
                            }
                            if (data == 400){
                                alert("email or password empty");
                            }
                            if(data == 403){
                                alert("passwords do not match");
                            }
                        }
                    });
                });
            });
      </script>

您正试图将从请求中返回的数据与HTTP状态代码进行比较。所以,你需要做的是在你的成功函数中加入一些额外的参数。这是我在另一个stackoverflow问题上看到的一个很好的Fiddle,它可能会帮助你。http://jsfiddle.net/magicaj/55HQq/3/.

$.ajax({
    url: "/echo/xml/",
    type: "POST",
    data: {
        //Set an empty response to see the error
        xml: "<response></response>"   
    },
    dataType:"text xml",
    success: function(xml, textStatus, xhr) {
        console.log(arguments);
        console.log(xhr.status);
    }
});

xhr.status是您需要比较的,而不是您的数据。

在玩console.log时,我发现从后端发送res.sendStatus(200)会导致客户端(浏览器)得到"OK"的响应。因此,当在服务器上更改为res.json时,它可以工作。。。