![](/img/trans.png)
[英]How to get content of an element in the html returned by jquery post
[英]How to get the returned value in jQuery .post()
我如何獲得返回值?
點擊我的按鈕調用:
$("#generateNumber").click(function(){
$.post( "./result.php", function( data ) {
console.log( data );
$(".generate_number").html(data);
});
});
我的result.php:
<?php
$a = "returnedValue";
?>
但我的控制台日志沒有顯示我的returnedValue。 為什么不?
您需要在result.php
:
<?php
$a = "returnedValue";
echo $a;
?>
您在jQuery.post()
中使用的回調中的data參數包含result.php
的輸出 。 但是,您的PHP腳本沒有輸出任何內容。 確保轉儲您希望AJAX調用讀取的任何數據。
如果你想在PHP腳本中調用一個特定的函數,你可能需要將一些參數傳遞給jQuery.post()
..一個簡單的例子:
$("#generateNumber").click(function(){
$.post("./result.php", {
functionName: 'foo'
}, function( data ) {
console.log( data );
// foo
});
});
在result.php
:
<?php
function foo() { return "foo"; }
function bar() { return "bar"; }
switch($_POST['functionName']) {
case 'foo': echo foo(); break;
case 'bar': echo bar(); break;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.