php – 在CodeIgniter中检索JSON POST数据

前端之家收集整理的这篇文章主要介绍了php – 在CodeIgniter中检索JSON POST数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试从我的PHP文件中检索 JSON数据.它让我很难.这是我的代码

我的视图中的代码

var productDetails = {'id':ISBNNumber,'qty':finalqty,'price':finalprice,'name':bookTitle};

        var base_url = '<?PHP echo site_url() ?>';
        $.ajax({
            url: "<?PHP echo base_url() ?>index.PHP/user/Add_to_cart/addProductsToCart",type: 'POST',data:productDetails,dataType:'JSON',});

试图在我的控制器中检索:

echo $this->input->post("productDetails");

输出什么都没有.

这是我的标题

Remote Address:[::1]:80
Request URL:http://localhost/CI/index.PHP/user/Add_to_cart/addProductsToCart
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json,text/javascript,*/*; q=0.01
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,fr;q=0.6
Connection:keep-alive
Content-Length:52
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ci_session=3E5SPro57IrJJkjs2feMNlmMrTqEXrTNN8UyEfleeothNnHwNxuCZDSx4a7cJZGjj7fyr2KLpj%2BPNJeGRSzSPVmcFHVEdhSk4D47ziOl4eZcTUAZlQrWa3EYIeQJVWxMpiGZS26MEfbSXNmfel9e8TcsJTreZHipvfisrJovbXEAW4Uv%2BwrJRep1KCi1MMaDCVJb9UEinRVcDtYe%2F86jhn7kOj4kraVmVzx%2FsOaO0rAxLyAUtez%2Feaa4zBwpN3Td153sAoIb3WxVHoEj2oKyH5prVHigbIhIBR6XZqjBkM6hjBuoD2OSZ2wgLbp9DEENMoqui4WYyHROBuS2DYiJajblcS0KiFga5k%2FQOODvE7p6n%2BozN5ciDliVjJ4PnJ5PD1GaPEmec5%2FbQSlOHYWZk%2F2Blzw3Nw0EtLL7wKDzzQY%3Df645c36bb3548eb8de915b73f8763d97a47783ce
Host:localhost
Origin:http://localhost
Referer:http://localhost/CI/index.PHP/user/view_available_books/viewAvailableBooks/5
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/40.0.2214.111 Safari/537.36
X-Requested-With:XMLHttpRequest
**Form Dataview** sourceview URL encoded
id:234
qty:1
price:0.00
name:dasdadsd2q3e!@!@@

我在Developer工具中可以看到的响应:

Array
(
    [id] => 234
    [qty] => 1
    [price] => 0.00
    [name] => dasdadsd2q3e!@!@@
)

但在浏览器中,输出什么都没有.我现在试图解决它超过4个小时但是徒劳无功.

print_r($_POST); // outputs nothing
echo $data = file_get_contents('PHP://input'); //outputs nothing
echo $id    = $this->input->post('productDetails');// outputs nothing

我的观点代码

<script>
    $('#addtoCart').on('click',function(event){
        event.preventDefault();
        $(this).attr('disabled',"disabled");
        finalprice = $.trim($('#price').val());
        finalqty = $.trim($('#quantity').val());

        var productDetails = JSON.stringify({'id':ISBNNumber,'name':bookTitle});

        var base_url = '<?PHP echo site_url() ?>';
        // console.log($);
        $.ajax({
            url: "<?PHP echo base_url() ?>index.PHP/user/Add_to_cart/addProductsToCart",contentType: "application/json; charset=utf-8",dataType:'html',});


    });
</script>

控制器代码

function addProductsToCart(){
        var_dump(json_decode(file_get_contents("PHP://input")));
        print_r($_POST);
        // $data = json_decode($_POST["productDetails"]);
        // var_dump($data);
        // echo $data = file_get_contents('PHP://input');
// print_r(json_decode($data));
        // $id    = $this->input->post('id');
        // $qty   = $this

    }
我用于CI中的Ajax调用的一般方法

JS:

post_array =
{
    "myvar" : "value1","myvar2": "value2"
} 

$.post(baseUrl + "/AjaxController/my_function",post_array,function(data)
    {
        var res = jQuery.parseJSON(data);
        alert(res.property);
    }

控制器:

public function my_function()
{
    $myvar = $this->input->post('myvar');
    $myvar2 = $this->input->post('myvar2'); 

    //Stuff

    echo json_encode($myobject);
}
原文链接:https://www.f2er.com/php/130517.html

猜你在找的PHP相关文章