Skip to main content
 首页 » 编程设计

javascript之未调用 AJAX 成功回调函数

2025年05月04日2652php

我在一个简单的网站上使用 python 和 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); 
        } 
    }); 

这是我的服务器端代码

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

服务器响应调用 - “POST/api/gtest/HTTP/1.1” 200 31

试图在这里解决类似的问题但没有成功:\ 无论我做什么,都只会调用错误函数。 错误警报始终为空。没有实际消息。

我知道这可能是一个小改动,但我找不到。

请您参考如下方法:

 $.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); 
    } 
});