簡體   English   中英

使用JQuery從PHP檢索數據

[英]Retrieve Data From PHP using JQuery

我不確定我做錯了什么,但我有一個PHP函數需要從中檢索一些數據。

我正在使用JQuery 1.11執行ajax函數

$(document).ready(function() {
   $('#butt_example').on('click', function() {
      $.ajax({
         url      : 'scripts/functions.php',
         type     : 'GET',
         dataType : 'json',
         data     : {
            'action': 'test'
         },
         success: function(result) {
            var la_result = JSON.parse(result);
            console.log(la_result);
         },
         error: function(log) {
            console.log(log.message);
         }
      });
   });
});

然后我用一個簡單的php函數替換了它,僅用於測試

<?php
if(isset($_GET["action"])) {
   $arr = array();
   $arr[0] = "test";
   $arr[1] = "passed";

   header('Content-Type: application/json; charset=utf-8');
   echo json_encode($arr);
}
?>

但是,當我單擊按鈕執行代碼時,我看到控制台日志結果未定義。

為什么是這樣?

更新

在Mike發表評論之后,我意識到它實際上是在錯誤情況下執行控制台日志。

所以它在某處失敗了,我不確定為什么

您確定AJAX調用能夠到達PHP函數嗎?

嘗試此操作。在.php文件中首先執行此操作

這將產生類似於codeigniter或任何其他相對框架的base_url()。

codeigniter中的base_url

function url(){
  return sprintf(
    "%s://%s%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_NAME'],
    $_SERVER['REQUEST_URI']
  );
}

<script>
  var url="<?php echo url(); ?>"; 
</script>

然后在您的JS文件中提供URL

 $(document).ready(function() {
       $('#butt_example').on('click', function() {
          $.ajax({
             url      : url+'scripts/functions.php',
             type     : 'GET',
             dataType : 'json',
             data     : {
                'action': 'test'
             },
             success: function(result) {
                var la_result = JSON.parse(result);
                console.log(la_result);
             },
             error: function(log) {
                console.log(log.message);
             }
          });
       });
    });

如前所述,為http和https修改的網址提取。

正如您從手冊中看到

錯誤
類型:函數(jqXHR jqXHR,字符串textStatus,字符串errorThrown)

第一個參數是jqXHR對象。

同樣從這里

為了與XMLHttpRequest向后兼容,一個jqXHR對象將公開以下屬性和方法:

  • readyState
  • 狀態
  • 當基礎請求分別以xml和/或文本作為響應時,statusText responseXML和/或responseText分別為setRequestHeader(name,value),該請求通過使用新值替換舊值而不是將新值連接到舊值而偏離標准
  • getAllResponseHeaders()
  • getResponseHeader()
  • statusCode()
  • abort()

如您所見,沒有jqXHR.message屬性。 因此,當您這樣做時:

     error: function(log) {
        console.log(log.message);
     }

當然,您將收到未定義的通知,因為該屬性不存在。

從此答案中 ,要獲取錯誤消息,您需要執行以下操作:

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

適用於我與jQuery 1.12.3

success: function(result) {
        alert(result);
},

//顯示“測試通過”

控制台日志中返回下一行JSON:JSON.parse:JSON數據的第1行第1行出現意外的關鍵字

似乎錯誤與json_encode函數有關。

CentOS 5 php安裝不包括json.so模塊,因此必須手動安裝它,然后才能修復錯誤。

感謝大家的幫助,對此深表感謝

嘗試這個:

$(document).ready(function() {
   $('#butt_example').on('click', function() {
      $.ajax({
         url      : 'scripts/functions.php',
         type     : 'GET',
         dataType : 'json',
         data     : 'action=test',
         success: function(result) {
            var la_result = JSON.parse(result);
            console.log(la_result);
         },
         error: function(log) {
            console.log(log.message);
         }
      });
   });
});

暫無
暫無

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

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