ajax XHR加载失败:POST

时间:2016-10-27 06:17:30

标签: javascript jquery ajax post xmlhttprequest

所以我想用ajax发帖,它就不会通过了。

这是我的javascript

$.fn.serializeObject = function()
        {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };

        $(function() {
            $('#formulario').submit(function() {
                formData = JSON.stringify($('#formulario').serializeObject());
                console.log(formData);
                //return false;

                                    $.ajax({
              type: "POST",
              url: "http://localhost/teleinformatica/API/user/validarLogin",
              data: formData,
              success: function(data){console.log(data.msg);},
              error: function(){location.reload();},
              dataType: "json",
              contentType : "application/json"
            });


            });
        });

我没有收到错误,也没有从ajax获得成功。但我收到上面列出的Chrome控制台错误XHR加载失败:POST

有时候帖子是正确完成的,其他人我得到XHR加载失败:POST

这是错误

XHR failed loading: POST 
"http://localhost/teleinformatica/API/user/validarLogin".
   send @ script/jquery.min.js:2
   ajax @ script/jquery.min.js:2
   (anonymous function) @ p2.php:89
   dispatch @ script/jquery.min.js:2
   u @ script/jquery.min.js:2

对此事的任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

检查您的数据必须

键入:PlainObject或String或Array &安培; success方法必须至少有一个参数

类型:功能(任何数据,字符串textStatus,jqXHR jqXHR)

http://api.jquery.com/jquery.ajax/ 例如:

formData = {data1:"yourdata1", data2:1021};
$.ajax({
    type: "POST",
    url: "http://localhost/teleinformatica/API/user/validarLogin",
    data: formData,
    success: function(data){console.log(data);},
   dataType: "json",
   contentType : "application/json"
});