我刚刚测试了我制作的脚本。它是在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/