Javascript不执行ajaxc

2024-04-27 02:18:02 发布

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

我有一个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中调用函数的方式如下:

<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()

Tags: the函数textform脚本jsondatahtml
2条回答

url属性中缺少,

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

        }
    });
}

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

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

而不是

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

相关问题 更多 >