react-native - Fetch post method is not working - react native
问题描述
I've a strange issue. Here the 1st method doesn't work. Here it gives error on the key username. It gives username is required error. However the 2nd method works. Both are basically the same. What might be the problem here
P.S the api in server is simple sql. I've not used multipart upload in the code.
1st function
test = () => {
fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json', //putting multipart/form-data doesn't work as well
},
body: JSON.stringify({
username: 'abc',
}),
})
.then((response) => console.log('fetchResponse', response))
.catch((error) => {
console.error('fetchError', error);
});
}
2nd function
test = () => {
let data = new FormData();
data.append("username", "abc");
fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
})
.then((response) => console.log('fetchResponse', response))
.catch((error) => {
console.error('fetchError', error);
});
}
Server Side code:
Controller
public function index_post(){
$username = $this->post('username', TRUE, TRUE);
$password = $this->post('password', TRUE, TRUE);
$push_key = $this->post('push_key');
$user = $this->login->checklogin($username, $password);
if ($user['status']) {
if(!$user['customer']->model_image){
$user['customer']->model_image='';
}
if(!$user['customer']->car_image){
$user['customer']->car_image='';
}
return $this->responsedata($user['customer']);
} else {
return $this->responseerror($user['msg']);
}
}
Model
public function checkLogin($username, $password){
$where= "((`password` ='".md5($password)."') or (`p_reset_code` = '".$password."'))";
$data = array();
$username = str_replace(' ', '', strtolower($username));
$this->db->where('username', $username);
$this->db->where($where);
$user = $this->db->get('tbl_appusers');
if ($user->num_rows() > 0) {
if (!$user->row()->is_logged_in) {
if($password == $user->row()->p_reset_code){
if($user->row()->p_reset_date == date('Y-m-d')){
$no= strval($user->row()->p_reset_no);
}else{
$no = '0';
}
$newpassword=$user->row()->temp_password;
$arr=array('is_logged_in'=>0,
'p_reset_code'=>'',
'p_reset_no'=>$no,
'temp_password'=>'',
'password'=>$newpassword
);
sendmail_password_reset_success($user->row()->email);
}else{
$arr= array('is_logged_in'=>0);
}
$this->db->where('customer_id', $user->row()->customer_id)->update('tbl_appusers', $arr);
$customer = $this->db
->select('tbl_customers.customer_id,tbl_customers.email, tbl_customers.salesdate, tbl_customers.name, tbl_customers.address, tbl_customers.cellphone, tbl_customers.scheme, tbl_customers.vcn, tbl_customers.ven, tbl_customers.model, tbl_customers.varient, tbl_customers.vehicleid, tbl_customers.color, tbl_customers.registrationno,tbl_customers.profile_image')
tbl_customers.scheme, tbl_customers.vcn, tbl_customers.ven, tbl_customers.vehicleid,tbl_customers.model, tbl_customers.varient, tbl_customers.color, tbl_customers.registrationno,tbl_customers.profile_image')
->join('tbl_appusers', 'tbl_appusers.customer_id=tbl_customers.customer_id')
->join('tbl_model_images', 'tbl_model_images.model=tbl_customers.model', LEFT)
->where('tbl_customers.customer_id', $user->row()->customer_id)
->get('tbl_customers')->row();
$this->db = $this->load->database('db2', true);
$arra=array('vehicleid'=>$customer->vehicleid);
$this->db->select('model_image, car_image')->from('mtable_vehicles')->where($arra);
$vehicles = $this->db->get()->row();
$customer->model_image=@$vehicles->model_image;
$customer->car_image=@$vehicles->car_image;
$data['status'] = true;
if(!$customer->profile_image){$customer->profile_image='';}
$data['customer'] = $customer;
} else {
$data['status'] = false;
$data['msg'] = "Already logged in";
}
} else {
$data['status'] = false;
$data['msg'] = "username and password does not match in our system please try again";
}
return $data;
}
I'm getting the following response in console
fetchResponse
{data: {…}, status: 200, statusText: undefined, headers: {…}, config: {…}, …}
config
:
{transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", adapter: ƒ, …}
data
:
{status: false, data: "username is required"}
headers
:
{transfer-encoding: "chunked", connection: "Keep-Alive", content-type: "application/json", set-cookie: Array(1), server: "Apache", …}
request
:
XMLHttpRequest {UNSENT: 0, OPENED: 1, HEADERS_RECEIVED: 2, LOADING: 3, DONE: 4, …}
status
:
200
statusText
:
undefined
__proto__
:
Object
解决方案
我记得我在与之交互的 PHP API 上遇到了类似的问题。
我知道这听起来有点疯狂,但你可以试试这个:
test = () => {
fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json', //putting multipart/form-data doesn't work as well
},
body: "?username=abc"
})
.then((response) => console.log('fetchResponse', response))
.catch((error) => {
console.error('fetchError', error);
});
}
您应该能够查看您的 API 是否接受了这个。
推荐阅读
- cas - 客户端如何使用更高的 cas 协议接收服务器传递的属性?
- node.js - 检索 Linkedin 用户的信息
- c++ - C ++将常量添加到向量切片
- python - Tensorflow:在纪元期间交换迭代器而不重新初始化
- database - 是否可以在 Firestore 上添加一个图层,以便不会直接提交更改?
- node.js - 使用自定义 ClassDecorator 在类实例中注入依赖项
- php - Symfony 3 - 根据主机名进行路由
- sed - 如何使用 sed 取消注释下一行
- php - 如何区分同一测试的每个实例
- reactjs - componentDidMount 生命周期方法在初始组件加载期间被调用两次(rendered-removed-rendered)