[英]Get server-side php output through select2 x-editable
我正在使用select2 v3.5.4和X可編輯v1.5.0
我有一個使用select2
通過POST發送數據到服務器上運行的PHP腳本的editable
,我正在嘗試將服務器輸出打印到js控制台中。 我正在嘗試使用success()
函數,但是在選擇值並單擊確認按鈕后,我在Chrome控制台中獲得的所有內容都是undefined
。
Javascript:
$(".sel").editable( {
source : [{id: '1', text: 'One'},
{id: '2', text: 'Two'},
{id: '3', text: 'Three'}],
select2 : {multiple: true},
url : "animalsDetailsEdit.php",
dataType: 'JSON',
success : function(response, newValue) {
console.log(response);
},
error : function(){
console.log("Error");
}
});
服務器端PHP:
<?php
echo json_encode("TEST MESSAGE");
?>
我嘗試了多種參數組合,但無濟於事。
我通常會設置PHP輸出的標頭,以便javascript可以輕松讀取輸出,還可以禁用錯誤報告功能,因此API變得非常適合Javascript讀取。
<?php
error_reporting(0);
...
...
...
header('Content-Type:application/json;');
echo json_encode("TEST MESSAGE");
您還可以在標題中設置字符集,例如“ UTF-8”等。 這個輸出很容易被javascript讀取...我注意到的另一件事是,測試消息是字符串而不是數組,我建議您這樣做。
<?php
error_reporting(0);
$output = array();
$output['status'] = true;
$output['message'] = 'Test Message';
header('Content-Type:application/json;');
echo json_encode($output);
在Javascript中,您可以使用輸出數組來檢查狀態和消息。 或者您可以為錯誤設置另一個變量,也可以在其中放置錯誤消息...
<?php
$output['status'] = false;
$output['error'] = 'error message';
$output['data'] = array();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.