[英]can't receive JS variable values in PHP using AJAX
我正在嘗試將Javascript變量發送到php文件。 在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
});
});
和PHP代碼:
<?php
$src1= $_POST['source1'];
$src2= $_POST['source2'];
echo $src1;
echo $src2;
?>
嘗試使用:
method: "POST"
不能代替,也不可以使用參數類型。 從jQuery文檔:
來自文檔
如果您使用的是1.9.0之前的jQuery版本,則應使用type。
但這並不意味着類型在新版本中不起作用,至少這是我所經歷的。
嘗試這個
1)創建文件output.log
在你的PHP文件中
$data = file_get_contents('php://input');
$stream = json_encode($data);
$request = json_encode($_REQUEST);
file_put_contents('output.log', "$stream\n$request);
如果沒有輸出,則說明您的網頁和php之間有問題。 大概路線?
發送對象時,可能會得到一個流
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.