Kleajmp
Kleajmp

Reputation: 167

ajax call to php script returns error: [object Object]

I'm trying to pass 5 strings and some uploaded images as FormData with AJAX to a php script, the same php-file as the one that executes the ajax code (isolated by if($mode="edit"){ ... });

php file containing all the code: ?page=shop&mode=edit

ajax code:

$.ajax({
        url: "?page=shop&mode=edit",
        type: "POST",
        dataType: 'multipart/form-data',
        data:  formData,
        contentType: false,
        cache: false,
        processData: false,
        success: function(phpfeedback) { console.log('success: '+phpfeedback); },
        error: function(phpfeedback) { console.log('error: '+phpfeedback); }
    });

php code:

if($mode=='edit'){

    //php code that logs all feedback into $ajax_feedback
    //I skip the phpcode as it is very long but it should be correct

    echo $ajax_feedback;
}

when I execute the ajax code I get: error: [object Object] in the console.

Why don't I see the $ajax_feedback string? Please ask me if you need any more information, I really searched for hours on this.

Upvotes: 1

Views: 780

Answers (1)

Kleajmp
Kleajmp

Reputation: 167

Thanks to a great comment I figured out that dataType is to specify the return format, not the format of data you are sending to the PHP code. As I wanted to return the content of a text string containing the error info of the php script I had to change the dataType from multipart/form-data to text.

$.ajax({
        dataType: 'text',
 });

Now I get a whole html document back from php (which I figured out is normal) but I would only want to have the content of one php string variable containing the error info back ($ajax_feedback). But i guess that is a new question.

Upvotes: 1

Related Questions