简体   繁体   English

django json响应和ajax调用

[英]django json response and ajax call

Kindly let me know how to consume django api method using jquery ajax call, because when I try to access directly URL am getting response, however when I send request using jquery ajax call, it goes to error function. 请让我知道如何使用jquery ajax调用来使用django api方法,因为当我尝试直接访问URL时会得到响应,但是当我使用jquery ajax调用发送请求时,它将转到错误功能。

Dajango code: Dajango代码:

def rain_api(request):
data = {}
data['test'] = 'hello'
return HttpResponse(json.dumps(data), content_type = "application/json")

JQuery code: jQuery代码:

requestURL="http://ipaddress/raindata";

$.ajax({
            url: requestURL,
              dataType: "json", 

            success:function(result){
                console.log(result)

            },
            done:function(result){
                 console.log(result)
            },
            error:function(error){
                console.log(error);

            }

        })

Try this out 试试看

 $.ajax({
               type: 'POST',
               url: 'http://ipaddress/raindata',
               processData: true,
               dataType: 'json',
               success: function(data ) {

                 console.log("Success")


                   }
               });
    }

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

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