JQuery的阿贾克斯POST没有发出请求抛出一个空的错误抛出、错误、JQuery、阿贾克斯

2023-09-10 19:19:44 作者:过分喜欢

我有一个函数,使一个Ajax请求的任何锚。请求方法可以GET或POST。在这种情况下,我想打一个POST不使用的一种形式,但即使在发送请求之前Ajax请求抛出一个错误。错误的值为错误和所有的错误/故障描述变量是

I have a function that makes an Ajax request for any anchor. The request method can be GET or POST. In this case, I want to make a POST without using a form but the Ajax request throws an error before even sending the request. The error has the value "error" and all error/failure description variables are "".

 function loadPage(url,elem_id,method,data) {
    ajaxLoading(elem_id);
    $.ajax({
        type: method,
        url: url,
        data: data,
        success:function(data){
            $("#"+elem_id).html(data);;
        },
        error:function(request,textStatus,error){
            alert(error);
        }
    });
 }

当函数被调用的PARAMS被这些(从JS控制台复制):

When the function is called the params are these (copied from the js console):

data: "partial=yes"
elem_id: "page"
method: "post"
url: "/projects/2/follow"

至于问,这里是code调用loadPage功能。

As asked, here is the code that calls the loadPage function.

$("body").on("click","a.ajax",function(event) {
    var _elem = getDataElem($(this));
    var _method = getRequestMethod($(this));
    var _partial = getRequestPartial($(this));
    handlers.do_request(event,$(this).attr("href"),_elem, _method, _partial);
});

var handlers = (function() {
    var obj = {};
    obj.do_request = function(event,url,elem_id,method,data) {
        event.preventDefault();

        loadPage(url,elem_id,method,data);
        history.pushState({selector:elem_id,method:method,data:data},null,url);
    };
}());

在Ajax请求的失败,该请求被默认制成,它响应sucesss。在所有我看过,这似乎是一个有效的方法,使一个POST请求(即不需要表格)。

After the failure of the Ajax request, the request is made by default and it responds sucesss. In all I have read, this seems to be a valid way to make a POST request (that doesn't need a form).

我做得不对的功能?为什么错误信息是空的?

Am I doing something wrong in the function? Why is the error information empty?

感谢

编辑:

我一直在想,对于一个POST从形式的功能起作用,当变量数据是用序列化功能(如VAR数据= $(本).serialize();)。难道说数据时,我做一个POST没有一个形式的格式错误以某种方式?也许JQuery的Ajax的功能不接受一个简单的字符串,如部分= YES作为数据在一个职位做。任何对此的思考?

I have been thinking, for a POST from a "form" that function works, when the variable "data" is made with the serialize function (e.g. "var data = $(this).serialize();"). Could it be that the format of the "data" when I make a POST without a "form" is wrong in someway? Maybe the JQuery Ajax function doesn't accept a simple string like "partial=yes" as data when a POST is made. Any thoughts on this?

推荐答案

未能解决问题,任何一方都不可能找到为什么发生的原因。虽然,我找到了解决的方法,通过使用与方法POST锚的隐藏的空表单。对于一个形式,功能很好地工作。

Could not solve the problem, neither could find the reason why it was happening. Although, I found a way around, by using a hidden empty form instead of an anchor with the method 'POST'. For a form, the function worked nicely.

谢谢你的答案