See @jpossi Answer.
I am not accepting an answer just yet, so that somebody can shed some light on what might be the problem, although it's far fetched as the original POST
code is not present.
Alright, so this worked, I just had to comment out processData: false
. But since the actual function uses the POST
method, I tried going back to the original code : var data = new FormData ( );
keep the processData: false
and change to method : 'GET'
and guess what, it worked... I simple got back to the original POST
code, uncommenting a few lines and commenting out a few lines. what's going on ?
Here is the code, it's pretty simple but somehow I am not able to catch the $_POST data sent by the Ajax function. Infact, I am not able to send the correct post data.
Here I tried with GET
option and here is the result.
/**Javascript**/
var data = new FormData ( );
data.append ( 'unique_id', unique_id ); // This I checked, it is correct.
$.ajax ( {
method : 'GET', url : scriptUrl, data : data, cache : false, processData: false, contentType: false, dataType : 'json',
success : function ( data, textStatus, jqXHR )
{
if ( typeof data.error === 'undefined' ) { alert ( data ); }
else { alert ( 'cccsdsd' ); }
},
error : function ( jqXHR, textStatus, errorThrown )
{
alert ( textStatus );// This fires with Parseerror.
}
} );
/**PHP**/
if ( $this->input->get ( 'unique_id' ) ) // I am working with codeigniter.
{
$data ['message'] = 'My Message';
echo json_encode ( $data );
}
else
{
echo 'Something Else';
}
The Ajax never is successful, it always throws the parseerror
.
The firebug GET url turns out like this : http://localhost/mysite/Cart [object%20FormData]&_=1451738500443
The response sent by the server is Something Else
.
What am I doing wrong ?
jQuery.ajax() expected as "data": PlainObject or String or Array
FormData can be used for POST-Requests. (This is handled by Browsers, not jQuery). FormData can not be converted to a GET-String, as it is intended to handle cases like File-Uploads.
You should change data to:
var data = {'unique_id': unique_id};
or change from GET to POST
or (as of comments) change processData to true
instead of
var data = new FormData ( );
data.append ( 'unique_id', unique_id ); // This I checked, it is correct.
just use
var data = { unique_id : unique_id };
Where
var data = { index_name : index_value };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.