码迷,mamicode.com
首页 > Windows程序 > 详细

可用于nodejs的SuperAgent(ajax API)

时间:2017-05-29 16:01:25      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:post请求   code   pes   post   json   发送   查询   color   写入   

简单示例:
import request from ‘superagent‘;//引用声明
request.post(api)
    .withCredentials()//跨域
    .end((err, res) => {
        if (res.ok) {
            const json = JSON.parse(res.text);
        } else {
            console.log(‘获取失败‘);
        }
    });

 

1、get 方式

  当使用get请求传递查询字符串的时候,用.query()方法,传递一个对象就可以,下面的代码将产生一个/search?query=Manny&range=1..5&order=desc请求:

request
   .get(‘/search‘)
   .query({ query: ‘Manny‘ })
   .query({ range: ‘1..5‘ })
   .query({ order: ‘desc‘ })
   .end(function(res){

   });

  或者传一个单独的大对象:

request
  .get(‘/search‘)
  .query({ query: ‘Manny‘, range: ‘1..5‘, order: ‘desc‘ })
  .end(function(res){

  });

  .query()方法也允许传递字符串:

request
    .get(‘/querystring‘)
    .query(‘search=Manny&range=1..5‘)
    .end(function(res){

    });

  或者字符串拼接:

request
    .get(‘/querystring‘)
    .query(‘search=Manny‘)
    .query(‘range=1..5‘)
    .end(function(res){

    });

2、post 请求

  一个典型的json post请求看起来就像下面的那样,设置一个合适的Content-type头字段,然后写入一些数据,在这个例子里只是json字符串:

request.post(‘/user‘)
    .set(‘Content-Type‘, ‘application/json‘)
    .send(‘{"name":"tj","pet":"tobi"}‘)
    .end(callback)

  因为json非常通用,所以就作为默认的Content-type,下面的例子跟上面的一样:

request.post(‘/user‘)
    .send({ name: ‘tj‘, pet: ‘tobi‘ })
    .end(callback)

  或者调用多次.send()方法:

request.post(‘/user‘)
    .send({ name: ‘tj‘ })
    .send({ pet: ‘tobi‘ })
    .end(callback)

  默认发送字符串,将设置Content-typeapplication/x-www-form-urlencoded,多次调用将会通过&来连接,这里的结果为name=tj&pet=tobi:

request.post(‘/user‘)
    .send(‘name=tj‘)
    .send(‘pet=tobi‘)
    .end(callback);

  superagent的请求数据格式化是可以扩展的,不过默认支持formjson两种格式,想发送数据以application/x-www-form-urlencoded类型的话,则可以简单的调用.type()方法传递form参数就行,这里默认是json,下面的请求将会postname=tj&pet=tobi内容:

request.post(‘/user‘)
    .type(‘form‘)
    .send({ name: ‘tj‘ })
    .send({ pet: ‘tobi‘ })
    .end(callback)

3、设置content-type

  常见的方案是使用.set()方法:

request.post(‘/user‘)
   .set(‘Content-Type‘, ‘application/json‘)

  一个简便的方法是调用.type()方法,传递一个规范的MIME名称,包括type/subtype,或者一个简单的后缀就像xml,json,png这样,例如:

 request.post(‘/user‘)
   .type(‘application/json‘)

 request.post(‘/user‘)
   .type(‘json‘)

 request.post(‘/user‘)
   .type(‘png‘)

4、设置接受类型

   跟.type()简便方法一样,这里也可以调用.accept()方法来设置接受类型,这个值将会被request.types所引用,支持传递一个规范的MIME名称,包括type/subtype,或者一个简单的后缀就像xml,json,png这样,例如:

request.get(‘/user‘)
   .accept(‘application/json‘)

 request.get(‘/user‘)
   .accept(‘json‘)

 request.get(‘/user‘)
   .accept(‘png‘)

5、跨域

  .withCredentials()方法可以激活发送原始cookie的能力,不过只有在Access-Control-Allow-Origin不是一个通配符(*),并且Access-Control-Allow-Credentials为’true’的情况下才行.

request
  .get(‘http://localhost:4001/‘)
  .withCredentials()
  .end(function(res){
    assert(200 == res.status);
    assert(‘tobi‘ == res.text);
    next();
  })

 

可用于nodejs的SuperAgent(ajax API)

标签:post请求   code   pes   post   json   发送   查询   color   写入   

原文地址:http://www.cnblogs.com/luoxiaowei/p/6917711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!