簡體   English   中英

通過AJAX從PHP中回顯的Javascript代碼未運行

[英]Echoed Javascript Code from PHP Via AJAX Not Running

我的代碼旨在通過AJAX使用Google圖書API吸引輸入書籍的作者。 如果未輸入任何內容,則顯示“ Empty”。 它的實際作用是在輸入書后打印出Javascript代碼。 當沒有輸入任何內容時,它將按原樣打印“空”。 如何修改我的代碼以使所執行的Javascript執行並因此獲得輸入書的作者?

只是看到,我用echo "<script>document.getElementById('txtBox').innerHTML = 'Hello';</script>";替換了echo部分echo "<script>document.getElementById('txtBox').innerHTML = 'Hello';</script>"; 它還會打印出javascript代碼,因此我認為它與使用API​​無關。

getAuthor.html

 <!DOCTYPE html> <html> <body> <!-- Input --> <div class="form"> <form onsubmit="makeRequest(); return false"> <input type="text" id="inputText" name="inputText"> <input type="submit"> </form> </div> <br> <!-- Output --> <div class="txtBox"> <textarea id="txtBox"> </textarea> </div> <!-- AJAX to create output using jEcho.php file--> <script> function makeRequest() { httpRequest = new XMLHttpRequest(); console.log(httpRequest.responseText); httpRequest.onreadystatechange = function() { document.getElementById("txtBox").innerHTML = httpRequest.responseText; }; httpRequest.open("POST", "jEcho.php", true); httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); httpRequest.send("inputText=" + document.getElementById("inputText").value); } </script> </body> </html> 

jEcho.php

 <?php $input = $_POST["inputText"]; if ($input == "") { echo "Empty"; } else { // used to parse // eg The Rosie Project -> The+Rosie+Project $temp = str_replace(" ", "+", $input); // create appropiate source $scriptSource = "https://www.googleapis.com/books/v1/volumes?q=$temp&callback=handleResponse"; echo "<script> function handleResponse(response) { var item = response.items[0]; document.getElementById('txtBox').innerHTML = item.volumeInfo.authors[0]; } </script> <script src='$scriptSource'></script>"; } ?> 

鏈接

從PHP中回顯Javascript:

如何從PHP調用JavaScript函數?

從PHP回顯JavaScript

Google Books API:

https://developers.google.com/books/docs/v1/getting_started

https://developers.google.com/books/docs/v1/using

<script>元素僅在首次加載頁面時運行。 稍后創建的腳本元素(通過分配給元素的.innerHTML ,使用document.createElement()創建它們document.createElement()或以其他方式不執行。

如果要讓PHP腳本發送回代碼進行評估,則必須直接執行,例如:

httpRequest.onreadystatechange = function() {               
    eval(httpRequest.responseText);
};

(並從響應中刪除<script>標記。)

嘗試在jEcho.php文件中設置標題(未測試)

header('Content-Type: application/javascript'); 

我不允許發表評論:

我不確定是什么原因造成的,但是<script src='$scriptSource'></script>";可以在handleResponse函數之前調用。我不太確定是什么原因導致的是我最好的主意。

您還不僅可以在這樣的代碼中已經包含URL:(jEcho.php)

<?php
$input = $_POST["inputText"];

if ($input == "") {
    echo "Empty";
} else {
    // used to parse
    // e.g. The Rosie Project -> The+Rosie+Project
    $temp = str_replace(" ", "+", $input);    

    // create appropiate source
    //$scriptSource = "https://www.googleapis.com/books/v1/volumes?q=$temp&callback=handleResponse";

    echo "
          <script src='https://www.googleapis.com/books/v1/volumes?q=$temp&callback=handleResponse'></script>
          <script>
              function handleResponse(response) {
                 var item = response.items[0];
                 document.getElementById('txtBox').innerHTML = item.volumeInfo.authors[0];
              }
          </script>";
}
?>

暫無
暫無

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

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