randomsuffer
randomsuffer

Reputation: 383

jQuery ajax post request not sending data

I am sending post request to my php code using jQuery ajax, but it's not sending data.

var uploadData = {
    'email': email
};
$.ajax({
  url: './upload.php',
  method: 'POST',
  dataType: 'json',
  data: uploadData,
  contentType: "application/json; charset=utf-8",
  success: function(data, status, xhr) {
    console.log(xhr.responseText);
    return(true);
  },
  error: function(data, status, xhr) {
    console.log(xhr.responseText);
    return(false);
  }
});

I tried to add dataType:'json', and contentType: "application/json; charset=utf-8", tried to change post to get, but nothing changed.

enter image description here

Upvotes: 2

Views: 10287

Answers (4)

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13928

For future googlers, If you have been trying to send an array via post,and had initialised your array as

var blah = [];

Try making it an object as below, and see if it works.

var blah = {};

Upvotes: 1

sudip
sudip

Reputation: 2850

First of all, there is no need for JSON.stringify.

In $.ajax call, configurain object's member "method" is used if the jquery version is >= 1.9.0 and "type" is used for the older versions. ( http://api.jquery.com/jquery.ajax/ ) Here is a snippet :

$.ajax({
    url: './upload.php',
    method: "POST", //**'type' if jquery v < 1.9.0 **
    dataType: 'json',
    data: {
        email: "[email protected]"
      },
    .....................
    ................
  });

Upvotes: 0

Michał Szczech
Michał Szczech

Reputation: 456

You can get pure request payload using:

$raw = file_get_contents('php://input');

then you can decode it from json to array:

$data = json_decode($raw, true);

and print the array:

print_r($data);

Upvotes: 0

Ashry Ramadhan
Ashry Ramadhan

Reputation: 71

I think it's just like this:

$.ajax({
  url: './upload.php',
  type: 'POST',
  dataType: 'json',
  data: {
    'email': '[email protected]'
  },
  success: function(data) {
    return data;
  }
});

That code works for me.

Upvotes: 1

Related Questions