ajax发送数据,但php表示未索引

ajax发送数据,但php表示未索引,php,ajax,Php,Ajax,PHP文件: <?php $result = $_POST['ID']; echo($result); ?> 我还尝试了json\u encode()和其他方法,也尝试了不使用数据类型的方法 但是什么都不管用 我的PHP版本是7.1 请帮助……有多种方法可以做到这一点,这里有一种:- <script> $("'.P0").on('click', function(){ var xmlhttp ; if

PHP文件:

   <?php
    $result = $_POST['ID'];
    echo($result);    
   ?>
我还尝试了
json\u encode()
和其他方法,也尝试了不使用数据类型的方法 但是什么都不管用

我的PHP版本是7.1


请帮助……

有多种方法可以做到这一点,这里有一种:-

<script>
$("'.P0").on('click', function(){
        var xmlhttp ;
        if (window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest(); 
        }
        else{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","wishlist.php?ID="+pid[0],true); //Define array pid[] in some other place
        xmlhttp.send(); 
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var result = xmlhttp.responseText ;                     
                alert(result) ;
            }
        }           
    });     
});
</script>

$(“.P0”)。在('click',function()上{
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}
否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.open(“GET”、“wishlist.php?ID=“+pid[0],true);//在其他位置定义数组pid[]
xmlhttp.send();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var result=xmlhttp.responseText;
警报(结果);
}
}           
});     
});

希望这能起作用。

使用下面的代码

$('.P0').click(function(){
$.ajax({
    type : 'post',
    url : 'wishlist.php',
    data : {
        ID:pid[0],              // Change the data passing to this.
    },
    dataType : 'json',
    success : function(ar){
    alert(ar);  
    }
});
window.location = "http://localhost/FashionWorld/PAGES/wishlist.php";
});
你传递数据的方式是错误的。
尝试上述修改并检查其是否有效。

数据类型设置为JSON时,您需要以JSON格式发送数据

更新代码:

$('.P0').click(function(){
  $.ajax({
     type : 'post',
     url : 'wishlist.php',
     data : {"ID": pid[0]},
     dataType : 'json',
     success : function(ar){
        alert(ar);  
     }
  });
 window.location = "http://localhost/FashionWorld/PAGES/wishlist.php";
}))


在PHP文件上,您可以使用全局
$\u POST
变量读取此值

您在变量中遇到问题:

data : 'ID=' +  pid[0]
我建议您首先通过初始化值进行测试,例如

data : 'ID=1'
第二个例子:

    <input id='record_id' value='2'/>

<button class='PO'>Send<button>

<script>
var record_id= $('#record_id').val();
$('.P0').click(function(){
    $.ajax({
        type : 'post',
        url : 'wishlist.php',
        data : 'ID=' + record_id,
        success : function(ar){
        alert(ar);  
        }
    });
window.location = "http://localhost/FashionWorld/PAGES/wishlist.php";
});

</script>

发送
var record_id=$('#record_id').val();
$('.P0')。单击(函数(){
$.ajax({
键入:“post”,
url:'wishlist.php',
数据:“ID=”+记录ID,
成功:功能(ar){
警报(ar);
}
});
window.location=”http://localhost/FashionWorld/PAGES/wishlist.php";
});

请尝试此代码。。我添加了json.parse

$.ajax({
        type : 'post',
        url : 'wishlist.php',
        data : 'ID=' +  pid[0],
        dataType : 'json',
        success : function(ar){
        var xxx = JSON.parse(ar);
        alert(xxx);  
        }
    });

谢谢:)

您的数据传递错误<代码>数据:{ID:pid[0]},虽然我不确定您的
pid[0]
来自何处,但它仍然会导致未定义,因为我无法在您当前的帖子中看到它。将
data
的值声明为
data:'ID='+pid[0]
是错误的语法<代码>数据属性需要一个JSON对象。因此,应该将其更正为
数据:{ID:pid[0]}
。hungrykoala先生,我尝试了数据:{ID:10},但没有效果..这应该是
数据:{ID:“10”}
值应该包含在
中,而变量不应该。问题被发布为jQuery,但您发布了DOM答案。OP也在使用POST,但您使用的是POST GET。上面的一个应该可以工作,唯一的原因是pid变量,请正确设置它。不工作…success函数响应正确值,但php未定义索引ID错误尝试在不使用
数据类型的情况下发送:“json”
,然后查看它是否工作。另外,请让我们知道您是如何获得pid[0]的。注意:第2行G:\wamp64\www\FashionWorld\PAGES\wishlist.php中的未定义索引:ID。您是如何传递变量“ID”的,但请注意:第2行G:\wamp64\www\FashionWorld\PAGES\wishlist.php中的未定义索引:ID不起作用。但是success函数响应正确
$.ajax({
        type : 'post',
        url : 'wishlist.php',
        data : 'ID=' +  pid[0],
        dataType : 'json',
        success : function(ar){
        var xxx = JSON.parse(ar);
        alert(xxx);  
        }
    });