gpt4 book ai didi

javascript - 简单的 ajax GET 请求在我的 Django 网页上不起作用

转载 作者:行者123 更新时间:2023-11-30 12:07:09 24 4
gpt4 key购买 nike

我对 Django 和 Ajax 都很陌生(这是我第一次同时使用这两者),而且我的代码收到错误消息。基本上我有一个 HTML 按钮,在 Django 中的模板生成的网页上,它应该调用一个使用 jquery 分配的函数 onclick。单击时,该按钮应向具有指定 url 的网页发送 GET 请求。这就是我想要它做的所有事情,但是,它似乎不起作用。它不断抛出错误。以下是部分代码:

<button class="btn btn-primary toChange">Click me</button>

    $(".toChange").click(function(){
$.ajax({
url: 'localhost:8000/MyApp/ajax_test',
type: 'GET',
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
//alert("Something happened!")
});


</script>

我的观点是:

def test(request):
return HttpResponse("Congrats, you've got it!")

目前,我正在使用firefox 浏览localhost:8000 上的网页。如果我在单击按钮时查看控制台,这就是我所看到的。

{"readyState":0,"status":0,"statusText":"[Exception... \"<no message>\"   nsresult: \"0x805e0006 (<unknown>)\"  location: \"JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js :: .send :: line 5\"  data: no]"}

AJAX error: error : [Exception... "<no message>" nsresult: "0x805e0006 (<unknown>)" location: "JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js :: .send :: line 5" data: no]

我尝试研究错误,但我不确定,但这可能是 firefox 不允许请求通过的错误。有人可以帮帮我吗?

最佳答案

更改您的网址:

$(".toChange").click(function(){
$.ajax({
url: 'localhost:8000/MyApp/ajax_test', # change here

$(".toChange").click(function(){
$.ajax({
url: '/MyApp/ajax_test',

关于javascript - 简单的 ajax GET 请求在我的 Django 网页上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com