Javascript没有执行Ajax调用

时间:2013-12-07 05:45:58

标签: javascript jquery python html ajax

我有一个HTML页面,我在localhost上运行。我想从Python脚本访问一些数据并在我的HTML页面中使用它,函数中的Ajax调用不起作用,如果我删除它,程序运行完美。有什么帮助吗?

Javascript功能:

<script src="/js/jquery.min.js" type = "text/javascript" ></script> 
     <link rel="stylesheet" href="LoginStyle.css" type="text/css" />

 <script type = "text/javascript" >
function getData()
{
//Code doesn't even enter this function but when i remove the $.ajax part it enters the function
alert("I AM HERE");

$.ajax(
{
type: "GET",
url: "/cgi-bin/check.py" // Path of the script/code i want to run
success: function(response)
{
    $("#username").text(data); //this is where i should get the response, Not sure about the syntax i.e. whether i should 
//output in a <div> tag or a text box

}
});
}

以HTML格式调用HTML中的函数:

<form name="login-form" class="login-form" action="" method="post" onsubmit="getData();return false;"">
<input id="Uname" name="username" type="text" class="input username" placeholder="Username" />

Python脚本:

#!/usr/bin/python
from StringIO import StringIO
import json

def htmlData():
    content=json.loads('{"access": {"token": {"issued_at": "2013-04-18T14:40:23.299903", "expires": "2013-04-19T14:40:23Z", "id": "4c5ef01f52c7404fb5324c520d25d1fe"}}}')
data=json.dumps(content, indent=4, separators = (', ', ': '))
    print data
    return

htmlData()

2 个答案:

答案 0 :(得分:0)

您在网址属性中缺少,

function getData() {
    //Code does not even enter this function but when i remove the $.ajax part it enters the function
    alert("I AM HERE");

    $.ajax({
        type: "GET",
        //--> here missing , in url
        url: "/cgi-bin/check.py", // Path of the script/code i want to run
        success: function (response) {
            $("#username").text(data); //this is where i should get the response, Not sure about the syntax i.e. whether i should 
            //output in a <div> tag or a text box

        }
    });
}

答案 1 :(得分:0)

尝试使用,您错过了添加, url字符串结尾

url: "/cgi-bin/check.py",

而不是

 url: "/cgi-bin/check.py"