使用Javascript中的提取API接收和处理JSON [英] Receive and process JSON using fetch API in Javascript

查看:138
本文介绍了使用Javascript中的提取API接收和处理JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,当条件不足时,我的Django应用会发送带消息的JSON响应.

In my Project when conditions are insufficient my Django app send JSON response with message.

我使用此JsonResponse()指令

I use for this JsonResponse() directive,

代码:

data = {
    'is_taken_email': email
}
return JsonResponse(data)

现在,我想使用Javascript fetch API接收此JSON响应,例如显示警报.

Now I want using Javascript fetch API receive this JSON response and for example show alert.

我不知道如何使用提取API来执行此操作.我想写一个 listener ,他将等待我的JSON响应.

I don't know how to use fetch API to do this. I want to write a listener who will be waiting for my JSON response from Django App.

我尝试:

function reqListener() {
  var stack = JSON.parse(data);
  console.log(stack);
}

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;

我想将我的Django应用中的JSON与硬编码JSON进行比较:
例如: fetch( 'is_taken_email': email)->然后做点事情

I want to compare JSON from my Django app with hardcoded JSON:
For example: fetch( 'is_taken_email': email) - > then make something

从我的Django应用程序接收JSON,并通过AJAX进行接收:

receive JSON from my Django app and as AJAX make it:

success: function(data) { if (data.is_taken_email) { make something; }

提前谢谢!

推荐答案

在javascript的全局窗口作用域中提供了一个访存API,第一个参数是您API的URL,它是基于Promise的机制.

A fetch API is provided in the global window scope in javascript, with the first argument being the URL of your API, it's Promise-based mechanism.

简单示例

// url (required)
fetch('URL_OF_YOUR_API', {//options => (optional)
    method: 'get' //Get / POST / ...
}).then(function(response) {
    //response
}).catch(function(err) {
// Called if the server returns any errors
  console.log("Error:"+err);
});

如果您要接收 JSON响应

In your case, If you want to receive the JSON response

 fetch('YOUR_URL')
    .then(function(response){
         // response is a json string
        return response.json();// convert it to a pure JavaScript object
    })
    .then(function(data){
         //Process Your data  
      if (data.is_taken_email)   
           alert(data);
    })
    .catch(function(err) {
        console.log(err);
      });

使用基于XMLHttpRequest

function successListener() {  
  var data = JSON.parse(this.responseText);  
  alert("Name is: "+data[0].name);  
}

function failureListener(err) {  
  console.log('Request failed', err);  
}

var request = new XMLHttpRequest();  
request.onload = successListener;  
request.onerror = failureListener;  
request.open('get', 'https://jsonplaceholder.typicode.com/users',true);  
request.send();

将Listener用作setInterval的示例(我不确定您是否想做这样的事情,只是为了与您共享)

Example of Using Listener as setInterval (I'm not sure that you want to do something like this, it's just to share with you)

var listen = setInterval(function() {

  fetch('https://jsonplaceholder.typicode.com/users')
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      if (data[0].name)
        console.log(data[0].name);
    })
    .catch(function(err) {
      console.log(err);
    });

}, 2000);//2 second

我对Django不熟悉,但是我希望这可以对您有所帮助.

I am not familier with Django, but I hope this could help you.

这篇关于使用Javascript中的提取API接收和处理JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆