亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用ajax從php文件中獲取返回文本?

如何使用ajax從php文件中獲取返回文本?

LEATH 2023-07-20 17:13:11
我正在使用 ajax 調用 php 文件。我想做的就是從 php 文件中獲取一個值,然后用 javascript 對其進行測試。我已經嘗試了很多很多事情,但只能得到未定義的結果(來自下面的代碼)。如何從 php 文件返回“hello”到我的 javascript?jQuery.ajax({   type: "POST",   url: "the_file_to_call.php",   data: {userid: group_id_tb},   success: function(data) {        var the_returned_string = jQuery(data).html();        alert(the_returned_string)    }});PHP 文件:<?phpecho '<div id="id-query-result>"';echo "hello";echo "</div>";
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

您可以像這樣更改 PHP 內部的代碼


<?php

$queryResult = '<div id="id-query-result">hello</div>';


echo json_encode(['html' => $queryResult]);

然后,改變你的ajax調用


jQuery.ajax({

   type: "GET",

   url: "the_file_to_call.php",

   data: {userid: group_id_tb},

   dataType: 'json',

   success: function(data) {

        var the_returned_string = data.html;

        alert(the_returned_string);

   }

});


查看完整回答
反對 回復 2023-07-20
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

$.ajax({

  type: "POST",

  url: "the_file_to_call.php",

  data: {userid: group_id_tb},

  success: function(data) {

      $('body').append(data);

      var text = $('#id-query-result').text();

      alert(text);

      $('#id-query-result').remove()

  }

});

為什么不直接附加 php 文件的 HTML 響應,然后獲取相應的文本。之后您可以將其刪除。


查看完整回答
反對 回復 2023-07-20
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

有兩個更改需要完成:

  1. type由于您直接調用 PHP 文件,因此將其更改為“GET”。

  2. 刪除 success 函數中包裝的 jQuery 方法并添加.html為屬性

jQuery.ajax({

   type: "GET",

   url: "the_file_to_call.php",

   data: {userid: group_id_tb},

   success: function(data) {

        var the_returned_string = data.html

        alert(the_returned_string)

    }

});


查看完整回答
反對 回復 2023-07-20
  • 3 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號