简体   繁体   中英

Javascript function call to python file returns contents of file instead of data?

I am calling my file isbnAPI.py dynamically using Javascript. When the user leaves the form input, the javascript adds the isbn input to the string "isbnAPI.py?isbn=" and makes a GET request.

The issue I am running into is that the call to the python file is returning the entire python as I have it copied below. this.responseText literally returns the entire python file instead of the title.

Can someone help me figure out why this is happening?

#isbnAPI.py
import urllib
import json
import urlparse

#serviceurl is the base url waiting for isbn input of either 10 or 13 digits
serviceurl = 'https://www.googleapis.com/books/v1/volumes?q=isbn:'

#private Google Books API key altered for StackOverflow
key='&key=092j3k2j323kjnkjFAKEKEY'

fields='&fields=items(volumeInfo/title,volumeInfo/authors)'

#This is where the url should be coming in
link = request

parsed = urlparse.urlparse(link, allow_fragments=False)
isbn = urlparse.parse_qs(parsed.query)['isbn'][0]

url = serviceurl + isbn + key +fields

uh = urllib.urlopen(url)
data = uh.read()

try:
    js = json.loads(data)
except:
    js = None

print str(js["items"][0]["volumeInfo"]["title"])

HTML file w/ javascript in script tag for readability

    <br> 
        <input type="text" id="" name="isbn" value="<?php echo $Isbn;    unset($_SESSION['Isbn']); ?>" 
        placeholder="" onkeyup="getTitle(this.value)">
    <br>
<script>
            function getTitle(str) {
              var xhttp;
              if (str.length == 0) { 
                return;
              }
              xhttp = new XMLHttpRequest();
              xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    console.log(this);
                  document.getElementById("BookTitle").value = this.responseText;
                }
              };
              xhttp.open("GET", "isbnAPI.py?isbn="+str, true);
              xhttp.send();   
            }

任何人都想知道的答案是,我应该使用“返回”而不是“打印”将答案传达回我的javascript代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM