简体   繁体   English

将数据发送到另一个页面时,Ajax调用不起作用

[英]Ajax call not working when sending data to another page

Here is my code: 这是我的代码:

<div class="category" id="<?php echo $cat->term_id; ?>"><?php echo $cat->cat_name; ?>   </div>


$(".category").click(function(){

             var categ = $(this).attr('id');
                              alert(categ);


    ajax({
  type:'POST',
  url:'http://myweb.com/rel_notes/?page_id=238',
  data:'cat='+categ,
  success:function(data) {
    if(data) {  


    } else { // DO SOMETHING 
  }
       }
});       

    });

and the code behind the page which is receiving the posted data ( http://myweb.com//rel_notes/?page_id=238 ) is here: 接收发布数据的页面后面的代码( http://myweb.com//rel_notes/?page_id=238 )在这里:

<?php
if (isset($_POST['cat']))
{
$cat_id = $_POST['cat'];
echo "<script>alert('$cat_id')</script>";
}
else
$cat_id = NULL;
?>

Problem: It didn't get the value in $cat_id. 问题:它没有获得$ cat_id中的值。 I tried changing $_POST to $_GET but that didn't work too. 我尝试将$ _POST更改为$ _GET,但这也没有用。 So kindly help me where am i missing something? 因此,请帮助我我在哪里缺少什么?

$.ajax({
  type:'POST',
  data: {cat: categ},
  url:'http://myweb.com//rel_notes/?page_id=238',
  error: function() {
         alert("Data Error");
    },
  success:function(data) {
     if(data) {  
    } else { 
    }
}

}); });

This is not good way dude. 这不是好家伙。 None can make alert on server side. 没有人可以在服务器端发出警报。

You are doing alert code on the server side. 您正在服务器端执行警报代码。

Just replace 只需更换

<?php
if (isset($_POST['cat']))
{
$cat_id = $_POST['cat'];
echo "<script>alert('$cat_id')</script>";
}
else $cat_id = NULL;
?>

by 通过

<?php
if (isset($_POST['cat']))
{
    echo $cat_id = $_POST['cat'];

}
else {
    echo $cat_id = "";
}
?>

and alert the code like 并像这样警告代码

$(".category").click(function(){
    var categ = $(this).attr('id');
    alert(categ);

    ajax({
      type:'POST',
      url:'http://myweb.com/rel_notes/?page_id=238',
      data:'cat='+categ,
      success:function(data) {
        if(data != "") {  
          alert(data);
        }else { // DO SOMETHING 

         }
       }
    });       

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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