如何从php中传递值获取url ajax?

时间:2017-06-27 11:36:45

标签: javascript php jquery ajax

我试图将值从php传递到ajax url

这是我的代码:

<?php


$id = $_GET['id'];

echo id;

?>

javascript代码

$.ajax({
        url:"http://localhost:3000/product/"+'<?$id; ?>',

    type: "POST",
    dataType: 'json', 
    crossDomain: "true",      
    success: function (result) {
        if (result.type == false) {
            alert("Error occured:" + result.data);
            return false;
        }
        $.each(JSON.parse(result.data),function(index,obj){
      console.log(obj.id);

我收到了错误 http://localhost:3000/product/%3C?=的$ id;%20%

3 个答案:

答案 0 :(得分:1)

我认为问题出在参数名称中,请尝试:

TypeError: Cannot read property '_writableState' of undefined

如果你想从php获取id,请执行:

url:"http://localhost:3000/product?id=" + id',

答案 1 :(得分:0)

url:urlCall +'?' + $ .param({“id”:id}),

答案 2 :(得分:0)

试试这个:

<script>
var getid = '<?php echo $_GET['id'] ?>';
$.ajax({
    url:"http://localhost:3000/product?id="+getid',
    type: "POST",
    dataType: 'json', 
    crossDomain: "true",      
   success: function (result) {
        if (result.type == false) {
        alert("Error occured:" + result.data);
        return false;
    }
    $.each(JSON.parse(result.data),function(index,obj){
  console.log(obj.id);
});
</script>