为什么我的ajax调用不工作?

0 投票
2 回答
602 浏览
提问于 2025-04-17 23:59

我有一个HTML页面,它像一个控制模块,上面有一个按钮,点击这个按钮会运行一个Python脚本来触发警报。但是当我点击它的时候,什么也没发生。而且在开发者工具里没有看到任何错误。Python代码是可以正常工作的,所以问题不在脚本上,而是在我的ajax请求上。

这是我的代码:

<input type='button' class="btn btn-default" value='Alarm ON' id = 'alarm'>

<script>
        $("#alarm").click(function()
        {
            $.ajax({
            type: "GET",
            url: "lib/scripts/AlarmON.py",
            success: function(response){}
            });
        })
</script>

我知道如果这个请求能成功的话,我房间里的警报就会响。

编辑:对那些感兴趣的人,这是我的脚本:

import suds
from suds.client import Client
from suds.transport.http import HttpAuthenticated
from suds.sax.text import Raw

def main():
    #Sends a network message to device to trigger an alarm
    url = "http://foobar/WSDL/v4.0/iLON100.WSDL"
    client = Client(url, username='ilon', password='ilon', location = 'http://foobar/WSDL/iLON100.WSDL')
    xml = Raw('<Item><UCPTname>Net/MB485/PLC/Virtual Fb/y2</UCPTname><UCPTvalue>TRUE</UCPTvalue></Item>')
    client.service.Write(xml)

if __name__ == "__main__":
    main()

更新:我在success()里面加了document.write("hi"),如果请求成功的话,它会在页面上写“hi”,所以我不太确定到底发生了什么..

2 个回答

0

还要加上函数错误的部分,看看会发生什么:

<script>
    $("#alarm").click(function()
    {
      $.ajax({
        type: "GET",
        url: "lib/scripts/AlarmON.py",
        success: function(response){
            console.log(response);
        },
        error: function(xhr){
            var status = xhr.status;
            var text = xhr.statusText;
            console.log( status + ': ' + text);
        }
      });
    })
</script>
2

你不能直接那样执行一个Python脚本。可以看看这里的内容:http://docs.python.org/2/howto/webservers.html

撰写回答