简体   繁体   English

无法使用AJAX在PHP中接收JS变量值

[英]can't receive JS variable values in PHP using AJAX

I'm trying to send a Javascript variable to a php file. 我正在尝试将Javascript变量发送到php文件。 Everething is ok in the JS side, I receive a msg that data is sent, but in the php side nothing happen, here is my JS code: 在JS方面一切都很好,我收到了发送数据的消息,但是在php方面却什么也没有发生,这是我的JS代码:

$(".add-to-cart").click(function(event){


    event.preventDefault();

$.ajax({
type: "POST",// see also here
url: 'c.php',// and this path will be proper
data: {
       source1: "some text",
       source2: "some text 2"}
}).done(function( msg )
      {
       alert( "Data Saved: " + msg );// see alert is come or not
     });

});

and the php code: 和PHP代码:

<?php
$src1= $_POST['source1'];  
$src2= $_POST['source2'];     

echo $src1; 
echo $src2;
?>

Try it with: 尝试使用:

method: "POST"

Not instead or with the type param too. 不能代替,也不可以使用参数类型。 From jquery docs: 从jQuery文档:

From the docs 来自文档

You should use type if you're using versions of jQuery prior to 1.9.0. 如果您使用的是1.9.0之前的jQuery版本,则应使用type。

But it doesn't meantion type is not working in newer versions, at least this is what I experienced. 但这并不意味着类型在新版本中不起作用,至少这是我所经历的。

Try this 尝试这个

1) create a file output.log 1)创建文件output.log

In you PHP file 在你的PHP文件中

$data = file_get_contents('php://input');
$stream = json_encode($data);
$request = json_encode($_REQUEST);
file_put_contents('output.log', "$stream\n$request);

If non output, then is something wrong between your web page and php. 如果没有输出,则说明您的网页和php之间有问题。 probably routeS? 大概路线?

As you send a object, probably you get a stream 发送对象时,可能会得到一个流

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

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