Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
Php 如何将codeigniter restfull API与jsonp一起使用?_Php_Ajax_Json_Codeigniter_Jsonp - Fatal编程技术网

Php 如何将codeigniter restfull API与jsonp一起使用?

Php 如何将codeigniter restfull API与jsonp一起使用?,php,ajax,json,codeigniter,jsonp,Php,Ajax,Json,Codeigniter,Jsonp,我正在视图中执行一个简单的ajax调用: <script type="text/javascript"> function verifyLogin(){ var data = null; var email = document.getElementsByName( "email" )[0].value; var pwd = document.getElementsByName( "password" )[0].value;

我正在视图中执行一个简单的ajax调用:

<script type="text/javascript">
    function verifyLogin(){
        var data = null;

        var email = document.getElementsByName( "email" )[0].value;
        var pwd = document.getElementsByName( "password" )[0].value;  

        $.ajax({
            type : 'POST',           
            url : server_url + 'application/login', // Servlet URL           
            data:{
                'email':email,
                'pwd':pwd
            },
            timeout: 10000,
            dataType: 'jsonp',
            success : function(data) {
                if(data.logged_in){
                    //We set up the Secuity Key
                        //FOR BROWSER TESTING ONLY !!!!!!!!!!!!!
                    setCookie("key", data.user.key, 1);

                    //Redirect
                    window.location.href="home.php";

                } else {
                    alert("Invalid Login!!");
                        console.log( data );
                    if( data.errors ) {
                        //define
                        var error = {};
                        error.alert = data.errors;

                        //Append
                        var template = Handlebars.compile( $('#alertTemplate').html() );
                        $('#errors').empty().append( template(error) );

                        //Erase
                        error = {};
                    }
                }
            },
            error : function( xhr, type )
            {
                alert('server error occurred');
            } 
        });
    }
</script>

函数verifyLogin(){
var数据=null;
var email=document.getElementsByName(“电子邮件”)[0]。值;
var pwd=document.getElementsByName(“密码”)[0]。值;
$.ajax({
键入:“POST”,
url:server_url+‘应用程序/登录’,//Servlet url
数据:{
“电子邮件”:电子邮件,
“pwd”:pwd
},
超时:10000,
数据类型:“jsonp”,
成功:功能(数据){
如果(数据已登录){
//我们设置了Secuity密钥
//仅适用于浏览器测试!!!!!!!!!!!!!
setCookie(“key”,data.user.key,1);
//重定向
window.location.href=“home.php”;
}否则{
警报(“无效登录!!”;
控制台日志(数据);
if(data.errors){
//定义
var error={};
error.alert=data.errors;
//附加
var template=handlebar.compile($('#alertTemplate').html();
$('#errors').empty().append(模板(错误));
//抹去
错误={};
}
}
},
错误:函数(xhr,类型)
{
警报(“发生服务器错误”);
} 
});
}
我在CodeIgniter中以以下方式接收它:

public function login()
    {
        //Gestion du Formulaire
        $this->load->library( 'form_validation' );
        $this->form_validation->set_rules( 'email', 'email', 'trim|required|valid_email|callback__check_login' );
        $this->form_validation->set_rules( 'pwd', 'password', 'trim|required' );

        if( $this->form_validation->run() )
        {
        // the form has successfully validated
            $email = strtolower( $this->input->post( 'email' ) );

            //Use Doctrine to retrieve user by Pwd
            $em = $this->doctrine->em;
            $pwd = hash( 'sha256', $this->input->post( 'pwd' ) . $email );
            $user = $em->getRepository( 'Entity\User' )->findOneBy( array( 'email' => $email, 'password' => $pwd ) );

            if( $user )
            {
                //Everything is Fine !
                    //We generate a key
                $key = $this->key->create(10, 1);

                //We retrieve the entity freshly generated
                $key_entity = $em->getRepository( 'Entity\Security_Key' )->findOneBy( array( 'value' => $key ) );

                //We associate it to the user:
                $user->setPrivateKey( $key_entity );

                // We can now persist this entity:
                try
                {
                    $em->persist($user);
                    $em->flush();
                }
                catch(\PDOException $e)
                {
                    // Error When Persisting the Entity !!
                    $array = array(
                                   'errors' => "<p>Server Error</p>",
                                   'logged_in' => FALSE
                               );

                    $this->output
                         ->set_content_type( 'application/json' )
                         ->set_output( json_encode( $array ) );

                    return FALSE;
                }

                // End persisting entity / attach key-user


                // Everything is fine, send the data back ! 
                $user_array = array(
                                    'id' => $user->getId(),
                                    'name' => $user->getUsername(),
                                    'key' => $key
                                );

                $array = array(
                               'user' => $user_array,
                               'logged_in' => TRUE
                           );

                $this->output
                     ->set_content_type( 'application/json' )
                     ->set_output( json_encode( $array ) );

                return TRUE;
            }

            //We didn't fin any matches
            $this->output
                 ->set_content_type( 'application/json' )
                 ->set_output( json_encode( array(  'errors' => "<p>Wrong password / email combination</p>",
                                                    'logged_in' => FALSE 
                                                ) ) );
            return FALSE;

        }

        //Error in the Form validation
        $this->output
             ->set_content_type( 'application/json' )
             ->set_output( json_encode( array(  'errors' => validation_errors(),
                                                'logged_in' => FALSE 
                                              ) ) );
        return FALSE;
    }
公共函数登录()
{
//公式化手势
$this->load->library('form_validation');
$this->form_validation->set_rules('email','email','trim | required | valid|email | callback|check|login');
$this->form_validation->set_规则('pwd','password','trim | required');
如果($this->form\u validation->run())
{
//表单已成功验证
$email=strtolower($this->input->post('email'));
//使用条令通过Pwd检索用户
$em=$this->doctrine->em;
$pwd=hash('sha256',$this->input->post('pwd')。$email);
$user=$em->getRepository('Entity\user')->findOneBy(数组('email'=>$email,'password'=>$pwd));
如果($user)
{
//一切都很好!
//我们生成一个密钥
$key=$this->key->create(10,1);
//我们检索新生成的实体
$key\u entity=$em->getRepository('entity\Security\u key')->findOneBy(数组('value'=>$key));
//我们将其与用户关联:
$user->setPrivateKey($key\u实体);
//我们现在可以持久化此实体:
尝试
{
$em->persist($user);
$em->flush();
}
捕获(\p异常$e)
{
//保存实体时出错!!
$array=array(
“错误”=>“服务器错误”

“, “已登录”=>错误 ); $this->output ->设置内容类型('application/json') ->设置输出(json编码($array)); 返回FALSE; } //结束持久化实体/附加密钥用户 //一切都很好,把数据发回去! $user\u数组=数组( 'id'=>$user->getId(), 'name'=>$user->getUsername(), “key”=>$key ); $array=array( 'user'=>$user\u数组, “已登录”=>TRUE ); $this->output ->设置内容类型('application/json') ->设置输出(json编码($array)); 返回TRUE; } //我们没有找到任何火柴 $this->output ->设置内容类型('application/json') ->设置输出(json编码)(数组('errors'=>“错误的密码/电子邮件组合

”, “已登录”=>错误 ) ) ); 返回FALSE; } //表单验证中出现错误 $this->output ->设置内容类型('application/json') ->设置输出(json)编码(数组('errors'=>validation\u errors(), “已登录”=>错误 ) ) ); 返回FALSE; }
我做错了什么?我应该以特定的方式配置codeigniter吗?它在json上工作得非常好

我应该在视图或标题中更改某些内容吗


谢谢

您是否收到js错误?你在CodeIgniter中使用的是条令吗?我什么都没有收到..jsonp接受回调,你的输出现在是json而不是回调(因此不是jsonp)。