Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Axios:在POST请求主体中发送变量,而不是参数_Javascript_React Native_Axios - Fatal编程技术网

Javascript Axios:在POST请求主体中发送变量,而不是参数

Javascript Axios:在POST请求主体中发送变量,而不是参数,javascript,react-native,axios,Javascript,React Native,Axios,我正在处理一个项目,在这个项目中,我需要在请求主体中发送身份验证变量,而不是作为参数。我看到POST请求将第二个参数作为文档中的主体发送,但我在.NET服务中遇到了一个错误 _response: "{"error":"invalid_clientId","error_description":"ClientId should be sent."}" 当我没有在正文中发送参数时,我在PHP中遇到了相同的错误,但是这些值与我在这个POST请求中使用的值相同,所以我知道这些参数和值是正确的 以下是我

我正在处理一个项目,在这个项目中,我需要在请求主体中发送身份验证变量,而不是作为参数。我看到POST请求将第二个参数作为文档中的主体发送,但我在.NET服务中遇到了一个错误

_response: "{"error":"invalid_clientId","error_description":"ClientId should be sent."}"
当我没有在正文中发送参数时,我在PHP中遇到了相同的错误,但是这些值与我在这个POST请求中使用的值相同,所以我知道这些参数和值是正确的

以下是我所拥有的:

axios.post(`https://myawesomeapi.com`, 
{username:this.state.email_address, password:this.state.password, grant_type:'password', client_id:'thisAppsClientID'}, 
{headers: { 'content-type':'application/x-www-form-urlencoded', 'accept':'application/json' }}
).then( res => {
  let result = res.data;
  console.log(result);
})
.catch( (e) => {  console.log(e.response); });
当我运行console.log并检查错误响应时,我看到:

config:
   adapter: ƒ xhrAdapter(config)
   data: "{"username":"myusername","password":"mypassword!","grant_type":"password","client_id":"thisAppsClientID"}"
   headers: {Accept: "application/json, text/plain, */*", Content-Type: "application/x-www-form-urlencoded"}
...
以下是我在guzzle中得到的有用信息:

    $this->client->request('post', 'https://myawesomeapi.com',
['form_params' => ['username' => $input['username'], 'password' => $input['password'], 'client_id'=>'thisAppsClientID', 'grant_type'=>'password']],
['headers' => ['accept' => 'application/json','Content-Type' => 'application/x-www-form-urlencoded']]);

用户名、密码、授权类型和客户端id是否作为正文传递?我把如何发送标题搞砸了吗?谢谢,让我知道

我知道我在axios上也遇到过类似的问题,但我一直没有完全弄清楚。然而,我能够得到post请求来处理。请尝试下面的代码片段。我希望它能起作用

const dataForm = new FormData();
dataForm.append('username', this.state.email_address);
dataForm.append('password', this.state.password);
dataForm.append('grant_type', 'password');
dataForm.append('client_id', 'thisAppsClientID');

axios.post('https://myawesomeapi.com', dataForm)
.then(res => {
  let result = res.data;
  console.log(result);
})
.catch(e => { 
  console.log(e.response); 
});

我知道我在axios上也遇到过类似的问题,但我一直没有完全弄明白。然而,我能够得到post请求来处理。请尝试下面的代码片段。我希望它能起作用

const dataForm = new FormData();
dataForm.append('username', this.state.email_address);
dataForm.append('password', this.state.password);
dataForm.append('grant_type', 'password');
dataForm.append('client_id', 'thisAppsClientID');

axios.post('https://myawesomeapi.com', dataForm)
.then(res => {
  let result = res.data;
  console.log(result);
})
.catch(e => { 
  console.log(e.response); 
});

是,您发送的参数正确。您确定“application/x-www-form-urlencoded”内容类型吗?这是作为参数提供给我的,在Postman和Guzzle for PHP中似乎都可以使用。有什么不同的东西对JS更好吗?你试过用正确的大小写格式写标题吗?例如:“Content-Type”而不是“Content-Type”,您还可以尝试将主体字符串化
axios.post(…,JSON.stringify(data),…)
我为数据添加了JSON.stringify({用户名:this.state.email_地址,密码:this.state.password,grant_type:'password',客户端id:'thisAppsClientID'),但得到相同的错误…是的,您正确发送了参数。您确定“application/x-www-form-urlencoded”内容类型吗?这是作为参数提供给我的,在Postman和Guzzle for PHP中似乎都可以使用。有什么不同的东西对JS更好吗?你试过用正确的大小写格式写标题吗?例如:“Content-Type”而不是“Content-Type”,您还可以尝试将主体字符串化
axios.post(…,JSON.stringify(data),…)
我为数据添加了JSON.stringify({username:this.state.email\u地址,密码:this.state.password,grant\u type:'password',client\u id:'thisAppsClientID'}),但得到了相同的错误…感谢您的代码片段。它仍在抛出需要提供的相同clientID错误:/解决方案是使用qs库对数据进行编码:根据:谢谢,在我的例子中,后端不希望在post正文中用户名密码为json。添加作为表单参数发送的FormData并修复了此问题。感谢提供此代码段。它仍在抛出需要提供的相同clientID错误:/解决方案是使用qs库对数据进行编码:根据:谢谢,在我的例子中,后端不希望在post正文中用户名密码为json。添加作为表单参数发送的FormData并修复了该问题。