Ajax请求python脚本 [英] ajax request to python script

查看:139
本文介绍了Ajax请求python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚测试了一个脚本,我做。这是蟒蛇。我用ajax,发送请求和尝试,并得到结果。

i just tested out a script i made. it is in python. i use ajax, to send a request and try and get the result.

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。没有Web框架,比如Django的。

the python script uses CGI. no web framework like django.

当我这样做要求它只是打印蟒蛇文件的内容。没有code被执行。

when i do this request it just prints the contents of the python file. no code is executed.

推荐答案

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

You should use JQuery for that ... instead of writing your own Ajax request, it can be written in a line:

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

您可以阅读更多有关在这里: JqueryPost

You can read more about that here: JqueryPost

这篇关于Ajax请求python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆