3

as the title says I'm sending some post data via ajax. but I keep on getting errors, can anyone take a look at the code and explain why my ajax call keeps failing?

submitForm(jQuery('#priceCalc'), {name: 'thingdoto', value: "true"});

function submitForm(form, data) {
        var postData = form.serializeArray(),
            formURL = form.attr("action");

        postData.push(data);
        console.log(postData);
        jQuery.ajax({
                url : formURL,
                type: 'POST',
                dataType : "json",
                data: postData,
                success:function(data)
                {
                        jQuery('#priceTotal').html(data);
                },
                error: function()
                {
                        jQuery('#priceTotal').html('error');
                }
        });
}

EDIT: The ajax call returns the error, so it's just not succeeding. Don't know why.

11
  • It'd be, you know, useful to mention what these errors are. Commented Nov 26, 2013 at 20:57
  • Oh hey! Sorry it just returns the error, in the ajax call, so I just the string "error". The function does not succeed. Commented Nov 26, 2013 at 20:58
  • is the URL hit returning 2xx? Commented Nov 26, 2013 at 20:59
  • 1
    Are you sure you are getting a 200 response? What is in the response body? Is it actually parseable JSON? Commented Nov 26, 2013 at 21:10
  • 1
    Can you share that JSON block? Or at least run it through jsonlint.com to see if it is valid? Commented Nov 26, 2013 at 21:13

2 Answers 2

10

You're sending data as an array, not a JSON string.

You want to do something like this.

$("form#ID").submit(function(e){

    e.preventDefault();

    var data = {}
    var Form = this;

    //Gathering the Data
    //and removing undefined keys(buttons)
    $.each(this.elements, function(i, v){
            var input = $(v);
        data[input.attr("name")] = input.val();
        delete data["undefined"];
    });

    //Form Validation goes here....

    //Save Form Data........
    $.ajax({
        cache: false,
        url : ?,
        type: "POST",
        dataType : "json",
        data : JSON.stringify(data),
        context : Form,
        success : function(callback){
            //Where $(this) => context == FORM
            console.log(JSON.parse(callback));
            $(this).html("Success!");
        },
        error : function(){
            $(this).html("Error!");
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

This logic "Gathering the Data" works for text fields, but does not work for checkboxes. It always transfers the "value" of the checkbox element regardless whether the checkbox is checked or not.
0

Old question, but I just ran into this situation:

jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK

And found out that the problem (in my case) was that calling ajax from a button inside a form to send JSON seems to cause JQuery to misinterpret the server response -- and always consider it an error. My solution was to change the form to a div or to change the button tag to an a tag (using Bootstrap to render a as button)

This worked

<div>
    <button></button>
</div>

or

<form>
    <a></a>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.