简体   繁体   English

Ajax发布到PHP不起作用

[英]Ajax post to PHP not working

Hi i got a problem im doing a ajax post to a php file but in the php file its empty 嗨,我在将ajax发布到php文件时遇到了问题,但是在php文件中它为空

JS
google.maps.event.addListener(marker, 'click', function(marker, i) {
return function() {

     var rid = locations[i][4]; //get id to varible
     console.log(rid);
     $.ajax({
            url: uri+'/helper.php',
            type: 'post',
            data: {'referens': rid},
            success: function(data){
                console.log(rid);
                window.location = uri+'/helper.php';
            },error: function(data){
                alert('error'); 
            }
        });
    }
}(marker, i));

and my helper.php 和我的helper.php

<?php 
$referens = $_POST['referens']; 
echo $referens;
echo 1;
?>

the output in helper.php is only 1 and not my referens post helper.php中的输出仅为1,而不是我的referens post

what if i want to use it like this in same file with location.reload(); 如果我想在具有location.reload()的同一文件中使用这种方式怎么办?

 success: function(data){
                console.log(data);
                location.reload();
            },error: function(data){
                alert('error'); 
            }
        });
    }
}(marker, i));

</script>
<?php include_once('helper.php');
 var_dump($referens); ?>

and helper.php 和helper.php

<?php 
   $referens = $_REQUEST['referens']; 
   echo $referens;
   echo 1;

   ?>

Your code looks fine. 您的代码看起来不错。

You are printing the wrong variable. 您正在打印错误的变量。

Change 更改

success: function(data){
  console.log(rid);
  window.location = uri+'/helper.php';
}

To

success: function(data){
  console.log(data); // Here you are getting return in data not as rid
  window.location = uri+'/helper.php?rid='+rid; // See, pass rid here.
}

in helper.php 在helper.php中

<?php 
$referens = $_REQUEST['referens']; 
echo $referens;
echo 1;
?>

As per your comments on other answers and on your post, I would like to mention this: 根据您对其他答案和您的帖子的评论,我想提一下:

console.log(rid);
window.location = uri+'/helper.php';

In your success callback rid is 91 as it should be as per your comments and that is absolutely correct because at the php file you are trying to access a POST var. 在您的成功回调中rid91因为它应该按照您的注释,这是绝对正确的,因为在php文件中,您尝试访问POST变量。

But when this line executes window.location = uri+'/helper.php'; 但是当这一行执行window.location = uri+'/helper.php'; then location changes and it makes a GET request, so it fails. 然后位置发生变化并发出GET请求,因此失败。

To get this var at PHP end you should try with $_REQUEST('referens') and you have to send it with url like this: 要在PHP端获取此var,应尝试使用$_REQUEST('referens')并且必须使用以下网址发送它:

window.location = uri+'/helper.php?referens=' + rid;

and at your php end: 并在您的PHP端:

<?php 
    $referens = $_REQUEST['referens']; // <---change here.
    echo $referens;
    echo 1;
?>

From the docs [ $_REQUEST() ]: 从文档[ $_REQUEST() ]:

An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE . 关联数组,默认情况下包含$_GET, $_POST$_COOKIE

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

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