python - ajax 请求 python 脚本

标签 python ajax cgi

我刚刚测试了我制作的脚本。它是在Python中。我使用ajax发送请求并尝试获取结果。

function ajaxFunction(){
    var ajaxRequest;
    var e = document.getElementById("ktype");
    var ktype = e.options[e.selectedIndex].value;
    var acookie = document.getElementById("acookie").value;
alert (ktype +"\n" + acookie);
    try{
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function(){
        if (ajaxRequest.readyState < 4){
            document.getElementById("resp").innerHTML = "<center><img src='loading.gif' style='max-width:60px; max-height:60px;'></center>";
        }
        if(ajaxRequest.readyState == 4){
            document.getElementById("resp").innerHTML = ajaxRequest.responseText;
        }
    }
ajaxRequest.open("POST", "kindle.py", true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxRequest.send("amzcookie=" + acookie + "&ktype=" + ktype);
}

Python 脚本使用 CGI。没有像 django 这样的 Web 框架。

当我执行此请求时,它只会打印 python 文件的内容。没有执行任何代码。

最佳答案

您应该使用 JQuery ...而不是编写自己的 Ajax 请求,它可以写在一行中:

$.post('link-to-my-python-script',{data},
          function(answer){
                      // process your request here ..
                  });

您可以在这里阅读更多相关信息:JqueryPost

关于python - ajax 请求 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703660/

相关文章:

python - Selenium 滚动到底部无法正常工作

python - 如何禁止在 Python 中的 Selenium Webdriver 的新选项卡中打开页面?

jquery - JQuery Ajax 读取 JSON 时出现问题

jquery - 停止对事件发布请求

javascript - 使用 jquery 重新绑定(bind)事件不起作用

c - 如何使用c语言编写的cgi程序上传图片文件?

python - 当模型是 pandas dataframe 时使 QTableView 可编辑

python - 谷歌的 protorpc 坏了(元类错误)

apache2 - Apache (2) 抛出 "No such file or directory: exec of '/usr/lib/cgi-bin/fst.cgi' 失败”

perl - 为什么我需要为 IIS 而不是 Apache 显式输出 HTTP header ?