簡體   English   中英

發送數據ajax php

[英]Send data ajax php

嘗試使用ajax將變量發送到php。

js:

var test1 = "test"
$.ajax({
    type : "POST",
    url : "getid.php",
    data:  {test1 : test1},
    success: function() {
         console.log("message sent!");
    }
}); 

“發來的消息!” 進入控制台

PHP:

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

錯誤信息:

Notice: Undefined index: test1...

我真的沒有看到我在這里做錯了什么......有什么想法嗎?

做`時更新*

$.ajax({    
        type : "POST",
        url : "getid.php",
        data:  {"test1" : test1},
        success: function(msg) {
            console.log("message sent!");
            console.log(msg);
        }
}); 

記錄“測試”

仍然在PHP中得到相同的錯誤..

改變你的jQuery代碼:

var test1 = "test"
$.ajax({
    type : "post",
    url : "getid.php",
    data:  {"test1" : test1}, // this is the row that was causing the problem
    success: function(msg) {
         console.log(msg);
    }
}); 

你必須將test1放在引號中,因為它是一個包含“test”的定義變量,導致數據為{"test":"test"}

因為響應被賦予回調函數作為參數。 嘗試這樣的事情

var test1 = "test"
$.ajax({
    type : "POST",
    url : "getid.php",
    data:  {"test1" : test1},
    success: function(data) { // data argument here
         console.log("message sent!");
         console.log("data:",data); // log it out here 
    }
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM