简体   繁体   English

未调用AJAX成功回调函数

[英]AJAX success callback function not called

i'm working with python and js on a simple website. 我正在一个简单的网站上使用python和js。 i'm trying to call a method from the client side and get result, but no matter what i do success function isnt happening. 我正在尝试从客户端调用方法并获得结果,但是无论我做什么,成功功能都不会发生。

this is my JS 这是我的JS

 $.ajax({
        url: "http://127.0.0.1:8000/api/gtest/",
        type: "POST",
        data: { information : "You have a very nice website, sir."},
        dataType: "json",
        success: function(data) {
            alert ("post is success");
        },
        error: function(request,error) { 
            alert(request.responseText);
            alert(error);
        }
    });

this is my server side code 这是我的服务器端代码

def gtest(request):
    jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
    return HttpResponse(jsonValidateReturn, content_type='application/json', mimetype='application/json')   

The server responds to the call - "POST /api/gtest/ HTTP/1.1" 200 31 服务器响应该呼叫-“ POST / api / gtest / HTTP / 1.1” 200 31

tried to go over similar questions here but with no success :\\ no matter what I do, only the error function is called. 试图在这里解决类似的问题,但是没有成功:\\不管我做什么,都只会调用错误函数。 the error alert is always empty.. no actual message. 错误警报始终为空。没有实际消息。

I know this is probably a small change but I can't find it. 我知道这可能是一个很小的变化,但我找不到。

 $.ajax({
    url: "http://127.0.0.1:8000/api/gtest/",
    type: "POST",
    data: { 
         'information' : "You have a very nice website, sir.",
         'csrfmiddlewaretoken': '{{csrf_token}}'
    },
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data) {
        alert ("post is success");
    },
    error: function(request,error) { 
        alert(request.responseText);
        alert(error);
    }
});

i cant upvote mccannf's comment. 我无法支持mccannf的评论。

The problem was solved by the link he posted, i ran the html code from a file on my pc and i needed to load it from the server so link wont start with file:// but with http:// 通过他发布的链接解决了问题,我从PC上的文件中运行了html代码,我需要从服务器加载它,因此链接不会以file://开头,而以http://开头

best regards.. 最好的祝福..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM