对python脚本的ajax请求

2024-10-02 10:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚测试了我写的剧本。它是用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文件的内容。不执行任何代码。在


Tags: newifvaluevarfunctiondocumenttryxmlhttp
1条回答
网友
1楼 · 发布于 2024-10-02 10:30:00

您应该使用JQuery来实现这一点。。。不必编写您自己的Ajax请求,它可以写在一行中:

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

你可以在这里阅读更多信息:JqueryPost

相关问题 更多 >

    热门问题