【问题标题】:Fetch in react native doesn't send the post with codeIgniter API在 react native 中获取不会使用 codeIgniter API 发送帖子
【发布时间】:2018-12-25 11:52:32
【问题描述】:

Fetch post 不适用于使用 codeIgniter 编码的 json api。 get 方法有效,但 post 方法存在问题。在 react native 和 code igniter 之间无法识别密钥。任何帮助表示赞赏。谢谢你

 fetch('http://zzz.com/login', {
   method: 'POST',
   headers: {
     Accept: 'application/json',
    'Content-Type': 'application/json', 
  },
  body: JSON.stringify({
     username: 'abc',
   }),
 })
 .then(response => response.json())
 .then(responseJson => {
   console.log('responseJson', responseJson);
 })
 .catch((error) => {
   console.error('fetchError', error);
 });

CodeIgniter 控制器

public function login()

{
    $un = $this->input->post('username'); //why doesn't the key 'username' is working here
    echo json_encode($un);
}

控制台日志:

responseJson false

更新:

1) 使用 json_decode,报错 “fetchError SyntaxError:JSON 输入意外结束”

public function login()
{
   $Data = json_decode(file_get_contents('php://input'), false);
     echo $Data;
 }

2) 使用 json_encode 会产生以下结果: responseJson {“用户名”:“abc”}

public function login()
{
   $Data = json_encode(file_get_contents('php://input'), false);
     echo $Data;
 }

更新 1:

1) 仅使用 file_get_contents 将输出为:responseJson {username: "abc"}

public function login()
{
    $Data = (file_get_contents('php://input'));
    echo $Data;
 }

2) 在服务器代码中使用 var_dump 和 json_decode,它会在应用程序控制台中出现以下错误

public function login()
{
    $Data = json_decode(file_get_contents('php://input'), true);
    var_dump ($Data);
 }

错误:

fetchError SyntaxError: Unexpected token a in JSON at position 0
    at parse (<anonymous>)
    at tryCallOne (E:\zzz\node_modules\promise\setimmediate\core.js:37)
    at E:\zzz\node_modules\promise\setimmediate\core.js:123
    at E:\zzz\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:295
    at _callTimer (E:\zzz\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:152)
    at _callImmediatesPass (E:\zzz\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:200)
    at Object.callImmediates (E:\zzz\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:464)
    at MessageQueue.__callImmediates (E:\zzz\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:320)
    at E:\zzz\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135
    at MessageQueue.__guard (E:\zzz\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:297)

控制台记录响应如下在应用程序控制台中给出数组:

fetch('http://zzz.com/login', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
   body: JSON.stringify({
     username: 'abc',
   })
})
.then((response) => console.log('response', response))

控制台:

response 
Response {type: "default", status: 200, ok: true, statusText: undefined, headers: Headers, …}
headers:Headers {map: {…}}
ok:true
status:200
statusText:undefined
type:"default"
url:"http://zzz.com/login"
_bodyInit:"array(1) {↵  ["username"]=>↵  string(3) "abc"↵}↵"
_bodyText:"array(1) {↵  ["username"]=>↵  string(3) "abc"↵}↵"
__proto__:Object

【问题讨论】:

  • CodeIgniter 是否有请求正文 JSON 反序列化器?
  • 很可能不是,因此您可能必须使用json_decode(file_get_contents('php://input'), true) 之类的方式读取原始流。 echo 表示在登录动作中查看结果。我还要补充一点,在对输入进行进一步处理之前对其进行清理。
  • @kadeer 使用回显 json_decode 除了如果我使用 $a = json_encode(file_get_contents('php://input'), true);回声$a;它将输出作为 fetchResponsee {"username":"abc"}
  • 请查看上面的更新
  • 嗨,对不起,我应该建议用var_dump 代替echo。如果您收到“JSON 输入意外结束”,请在登录操作中查看以下输出:file_get_contents('php://input')。那应该以原始形式返回 POST 的内容。确保您看到的是有效的 JSON。

标签: codeigniter react-native


【解决方案1】:

尝试添加same-origin模式如下:

fetch('http://zzz.com/login', {
   method: 'POST',
   credentials: 'same-origin',
   mode: 'same-origin',
   headers: {
     'Accept': 'application/json',
     'Content-Type': 'application/json', 
   },
   body: JSON.stringify({
     username: 'abc',
   }),
 })
 .then(response => response.json())
 .then(responseJson => {
   console.log('responseJson', responseJson);
 })
 .catch((error) => {
   console.error('fetchError', error);
 });

【讨论】:

  • 恐怕没用。请看看上面的更新。谢谢你
  • 尝试发送没有json stringify的数据然后解码body: {username: 'abc'},
猜你喜欢
  • 1970-01-01
  • 2019-07-15
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多