当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Getting error trying to submit data using JQuery ajax

问题描述:

When I try to run the following JQuery ajax, I get an error saying that a parameter is missing and it will not send the data. I can't find any syntax errors. What am I doing wrong?

Here is the javascript with the JQuery ajax code:

function submitAction(actionname) {

if (actionname == "illustrationgenerate.htm") {

var thisForm = document.getElementById("illustrationTypeForm");

var fd = new FormData(thisForm);

$.ajax({

url: "illustrationgenerate.htm",

type: "POST",

data: fd,

datatype: "xml",

cache: false,

success: function (result, status, xhr) {

document.getElementById('errorMessage0').value="Success";

},

error: function (xhr, status, error) {

alert(xhr.status);

alert(request.responseText);

}

});

} else {

document.forms[0].action = actionname;

document.forms[0].method = "POST";

document.forms[0].target = '';

document.forms[0].submit();

}

}

网友答案:

Why not use jQuery native form encoder?

 $.ajax({
            ...
            data: $('#illustrationTypeForm').serializeArray(),
            ...

        });
网友答案:

Try This

Here is the javascript with the JQuery ajax code:

function submitAction(actionname) {     

    if (actionname == "illustrationgenerate.htm") {

        var thisForm = document.getElementById("illustrationTypeForm");
        var fd = new FormData(thisForm);
        $.ajax({
            url: "illustrationgenerate.htm",
            type: "POST",
            data: $('#illustrationTypeForm').serializeArray(),
            datatype: "xml",
            cache: false,
            success: function (result, status, xhr) {
                document.getElementById('errorMessage0').value="Success";
            },
            error: function (xhr, status, error) {
                alert(xhr.status);
                alert(request.responseText);
            }                           
        });

    } else {
        document.forms[0].action = actionname;
        document.forms[0].method = "POST";
        document.forms[0].target = '';
        document.forms[0].submit();
    }
}
网友答案:

To make ajax request using jQuery a type 'POST' you can do this by following code :

$.ajax({
    url: "test.php",
    type: "post",
    data: values ,
    success: function (response) {
       // you will get response from your php page (what you echo or print)                 
    },
    error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: