使用Javascript从php获取值到ajax请求

问题描述

向大家问好

我是 javaScript 的初学者,我对 ajax 有点陌生...我试图从 php 获取返回值,如错误消息或成功消息,并将其传递给用户。欢迎提出任何建议

PS:当我还没有开始使用 ajax 时一切正常

 $('button[post-request]').click(function() {
        // event.preventDefault();

        var request,address,form,response;
        address = $('form').attr('action');
        response = document.getElementById("return");

        $('input').prop('disabled',true);

        request = new XMLHttpRequest();
        request.open('POST',true);
        request.setRequestHeader('X-Requested-With','XMLHttpRequest');

        request.onreadystatechange = function(){
            if(this.readyState == 4 && this.status == 200){
                response.innerHTML = this.responseText;

                console.log(request.responseText);

            }
            else{
                console.log(request.statusText);
            }
        }

        request.send();


    });

这是我的php代码

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller
{

    public $data,$uid;

    public function __construct()
    {
        parent::__construct();

        // loading models
        $this->load->model(['logic/auth_action'=>'auth','validations/auth_validation'=>'verify']);

        // store ecncrypted user uid in a session
        $this->uid = $this->session->userdata('uid');

        $this->data['user'] = $this->usr->fetch_all_information($this->uid);
    }   

    public function login()
    {   
        $this->func->is_logged_in(true,'dashboard');

        $this->data['title'] = "Sign in";

        if(!empty($_POST) && $this->input->is_ajax_request()):
            // validating users inputs coming from the form
            $user_inputs = $this->verify->authenticate_userInputs('login');

            // checking if no error isset && carry on with the next step
            if(!isset($user_inputs['error_msgs'])):
                // performing neccessary action after validating
                $return = $this->auth->login($user_inputs);
            else:
                // store error for $return variable if there is any && pass it on
                $return = $this->func->return_validation_error($user_inputs);
            endif;
            // retrieve the error stored and display it to user
            print $this->func->fetch_message('error',$return);
        endif;

        // this display login page
        $this->load->view('auth/login',$this->data);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...