无法让简单的XMLHttpRequest工作

时间:2017-07-28 00:37:04

标签: javascript ajax forms xmlhttprequest

我正在努力让这项工作成为更大型练习的一部分。

这非常简单 - 用户填写使用XMLHttpRequest发送到处理页面的表单。这应该在表单下面返回一个响应。

我几乎工作了,但是一个领域不会显示......现在什么都没有。这可能是缓存问题还是代码问题。

以下是表格:

from bs4 import BeautifulSoup
import urllib.request


def SearchVid(search):
    responce = urllib.request.urlopen('https://www.youtube.com/results?search_query='+search)

    soup = BeautifulSoup(responce)    
    divs = soup.find_all("div", { "class" : "yt-lockup-content"})


    for i in divs:
        href= i.find('a', href=True)
        print(href.text,  "\nhttps://www.youtube.com"+href['href'], '\n')
        with open(SearchString.replace("%20", "_")+'.txt', 'a') as writer:
            writer.write("https://www.youtube.com"+href['href']+'\n')

print("What are you looking for?")
SearchString = input()
SearchVid(SearchString.replace(" ", "%20"))

使用Javascript:

<div id="miniContact">
<label for="yourName">Your Name</label>
<input type="text" name="yourName" id="yourName"><br>

<label for="phone">Your Phone</label>
<input type="text" name="phone" id="phone"><br>
<input type="text" name="reqd" id="reqd"><br>

<label for="email">Your Email</label>
<input type="email" name="email" id="email"><br>

<label for="type">Your vehicle type</label>
<input type="text" name="type" id="type">

<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>

...和php文件(my_parse_file.php):

<script>
    function ajax_post(){
        // Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
        // Create some variables we need to send to our PHP file
        var url = "my_parse_file.php";

        var fn = document.getElementById("first_name").value;

        var yourName    = document.getElementById("yourName").value;
        var phone       = document.getElementById("phone").value;
        var reg         = document.getElementById("reg").value;
        var srv     = document.getElementById("reqd").value;
        var email       = document.getElementById("email").value;
        var type        = document.getElementById("type").value;



        var vars = "yourName="+yourName+"&phone="+phone+"&reg="+reg+"srv="+service+"email="+email+"&type="+type;


        hr.open("POST", url, true);
        // Set content type header information for sending url encoded variables in the request
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // Access the onreadystatechange event for the XMLHttpRequest object
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("status").innerHTML = return_data;
            }
        }
        // Send the data to PHP now... and wait for response to update the status div
        hr.send(vars); // Actually execute the request
        document.getElementById("status").innerHTML = "processing...";
    }
    </script>

0 个答案:

没有答案