简体   繁体   中英

passing array from javascript to php

I am trying to pass an array() on PHP from JavaScript but PHP receives nothing. It always sets $str to "" . Why?

JavaScript

 var ArrayPassedID = [];
 function pass(){ 
    $.ajax({
        url: 'http://mysite/index2.php?task=getPassed',
        type:'get',
        dataType:'json',
        data: {id: JSON.stringify(ArrayPassedID)},
        async: false,
        success: function(response){ 
            ArrayPassedID.push(response.id);
    }

 ....

PHP

$str = "";
if(!empty($_POST["id"])){
    $id = $_POST["id"];
    $id = json_decode($id,true);
    $str = implode(",",$id);
}     

$data = query(SELECT id, response FROM `conversation` WHERE id not in ('".$str ."'));
$values = array();
$values['id'] = $data['id'];
$values['response'] = $data['response'];
return json_encode($values);

From Javascript, you send data with GET method of HTTP protocol. From PHP, you retrieve data from global $_POST , which correspond to POST method. Use same method in the two ways.

in ajax (js) :

type:"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