URL中带有对象参数的后期操作API [英] Post action API with object parameter within the URL

查看:159
本文介绍了URL中带有对象参数的后期操作API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API,其中某些参数需要在URL中给出. 我的api网址外观示例:https://www.server.com/api/actions/execute?auth_type=apikey&data={"Name": "name","Email" : "email"}

I've got an API where some of the parameters need to be given within the URL. Example of how my api url looks like: https://www.server.com/api/actions/execute?auth_type=apikey&data={"Name": "name","Email" : "email"}

我的代码现在是什么样子

What my code looks like right now

register = async () => {
    let data = {"Name":this.state.name, "Email":this.state.email}
    data = JSON.stringify(data)

    let URL = 'https://www.server.com/api/actions/execute?auth_type=apikey&data=';

    fetch(URL, {
      method: 'POST',
      headers: new Headers({
        'Content-Type': 'application/json'
      }),
      body: data
      })
      .then((response) => response.text())
      .then((responseText) => {
        alert(responseText);
      })
      .catch((error) => {
          console.error(error);
    });
  }

我在设备上收到的回复:

The response I get on my device:

{"code":"succes","details":{"userMessage":["java.lang.Object@2e56000c"],"output_type":void","id:"20620000000018001"},"message":"function executed succesfully"}

当我在邮递员中进行测试时,这一切都很好,但是我无法在React-Native中使用它.我已经尝试过'Content-Type':'application/x-www-form-urlencoded'之类的东西.

This is alle working fine when I test it in postman but I can't get it to work within React-Native. I've tried stuff like 'Content-Type':'application/x-www-form-urlencoded' already.

推荐答案

如果需要通过URL传递参数,则应使用GET,如果使用POST,则应在body

If you need to pass parameters via URL you should use GET, if you use POST then the parameters should be passed in the body

这篇关于URL中带有对象参数的后期操作API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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