提取发布方法不起作用-本机反应

时间:2018-12-24 09:47:55

标签: react-native

我有一个奇怪的问题。在这里,第一种方法不起作用。在这里,它给出了密钥用户名的错误。它给用户名是必需的错误。但是第二种方法有效。两者基本相同。这里可能是什么问题

P.S服务器中的api是简单的sql。我没有在代码中使用分段上传。

第一个功能

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);
 });
}

第二个功能

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);
 });
}

服务器端代码:

控制器

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']);
    }

}

型号

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;
}

我在控制台中收到以下响应

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

2 个答案:

答案 0 :(得分:0)

我也不是CodeIgniter专家,但是由于请求看起来不错,IMO,我想知道后端是否尝试以正确的方式访问变量。

查看CI文档,$this->input->post(....是正确的调用,而不是$this->post(...

https://www.codeigniter.com/user_guide/libraries/input.html#CI_Input::post

这可能是后端无法检测到用户名的原因吗?

通常,我建议您使用断点调试后端,以确保所有期望的数据都到达并且您以正确的方式对其进行处理。

答案 1 :(得分:0)

我记得我与之交互的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是否接受了。