zoukankan      html  css  js  c++  java
  • fetch获取json的正确姿势

    fetch要求参数传递,遇到请求无法正常获取数据,网上其他很多版本类似这样:

        fetch(url ,{
            method: 'POST',
            headers:{
                        'Accept': 'application/json, text/plain, */*',
                        'Content-Type': 'application/json'
            },
             body: JSON.stringify({a:1,b:2})
        }).then(function(response){
            return response.json();
        }).then(function(data){
             console.log(data);
        });

    经过改进和测试,如下:

    var ur = 'xxx',params = {page:1,rows:10},param='';
    for(var key in params){
      param += key + '=' + params[key] + '&';
    }
    if(param) param = param.substring(0,param.length-1);
    var requestConfig = {
      method: 'POST',
      credentials: 'include',
      headers: {
        'Accept':'application/json, text/plain, */*',
        'Content-Type': 'application/x-www-form-urlencoded'               
      }      
    };
    Object.defineProperty(requestConfig,'body',{
        value: param
    });
    fetch(url,requestConfig).then(function(res){
       return res.json(); 
    }).then(function(json){
       console.log(json.data);
    });
  • 相关阅读:
    数据库外键约束
    mysql查询数据
    操作mysql操作数据库
    自定义标签
    jstl标签
    getattibute 与 getparameter区别
    2017.3.2
    java中静态,抽象,接口,继承总结
    关于使用css伪类实现小图标
    动态生成的dom元素绑定事件
  • 原文地址:https://www.cnblogs.com/xtreme/p/7447059.html
Copyright © 2011-2022 走看看