javascript - send json into php using jquery -
i have json php file , want send output php file. javascript:
var data_1; $.ajax({ type:"post", url:"sd.php", success:function(result){ data_1 = result; test(data_1); var st = json.stringify(data_1); $.post('get.php',{q:st},function(data){ window.location = "get.php"; }); } });
and php file store json:
<?php $obj = json_decode($_post['q']); echo $obj; ?>
but outputs nothing. should do? please help.
you're not posting data with $.ajax
call. try this
the javascript
var req = $.post("sd.php", {foo: "bar"}); req.done(function(res) { console.log(res); }); req.fail(function() { console.log("there error"); });
the php
<?php // sd.php $data = $_post["foo"]; echo "foo set to: {$data}"; exit;
the output in browser console be
"foo set to: bar"
if want return json php, can like
<?php header("content-type: application/json"); $data = array("text" => "hello {$_post['foo']}"); echo json_encode($data); exit;
then output in browser console be
{text: "hello bar"}
Comments
Post a Comment