ajax在同一页面上向php发送请求

时间:2014-02-07 01:13:56

标签: javascript php jquery ajax post

我已经多次看过这个问题而且我已经尝试过实施解决方案,但这对我不起作用。你如何在同一页面上正确地发布ajax请求并在php中检索数据?

<?php 
error_reporting(-1);
var_dump($_REQUEST);
var_dump($_POST['img_url']); # ALWAYS NULL
?>

<script type='text/javascript' src='/steal/steal.js'></script>
<script type="text/javascript" src="/plugins/jquery/json2.js"></script>
<script type="text/javascript">
steal('jquery/dom/fixture').then(function(){

$.fixture("GET /event/{code}", function(original, settings, headers){
    return [200, "success", { "img_url":"http://path/to/img.png" }, {} ]
})

var strObj = <?php echo json_encode($_REQUEST); ?>;
var str = strObj.q;
var eventCode = str.split('/')[1];
console.log(eventCode);

$.ajax({
  url: "/event/"+eventCode,
  success: function(data) {
     //document.write('<img src="'+data.img_url+'"/>');
     $.ajax({
        type: 'POST',
        data: {img_url : data.img_url}
     });
   }
});

});
</script>

0 个答案:

没有答案