简体   繁体   中英

Ajax POST to PHP and receive response, jQuery

So, I have a JSON array that I want to POST to a file and then receive a response. But somehow it doesn't seem to work, so I was hoping you guys could help me a bit out.

data = JSON.stringify({
        "jsonrpc": "2.0",
        "method": "login",
        "id": 1,
        "params": {
          "params": {
            "username": "1234",
            "password":  "4321"
          }
        }
      });

$.ajax({
        url:"functions/proxy.php",
        type:"POST",
        data : data,
        success: function(data){/* do something*/ },
        error: function(data) {/* do something*/)}
      });

When I submit the form, the error function runs, and I get this when var_dumping $_POST :

array(0) {
}

Weird thing is, when data looks like this:

data: "username=1234&password&4321"

I get this:

Array
(
    [username] => 1291
    [password] => 1877
)

Could anyone help me with how I can be able to send the data in JSON format?

$.ajax({
        url:"functions/proxy.php",
        type:"POST",
        data : data,
        success: function(data){/* do something*/ },
        error: function(data) {/* do something*/)}
      });

should be

$.ajax({
        url:"functions/proxy.php",
        type:"POST",
        data : {mydata:data},
        success: function(data){/* do something*/ },
        error: function(data) {/* do something*/)}
      });

try

print_r($_POST);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM