简体   繁体   中英

AJAX success callback function not called

i'm working with python and js on a simple website. 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

 $.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

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.

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://

best regards..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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