从laravel项目返回json响应"无法读取Null属性"

时间:2016-02-22 12:00:44

标签: json laravel-5 response appcelerator-titanium

我发送电子邮件并获得对移动应用程序的回复之后,我在我的php文件(laravel)中有这个

 $msg ="email sent " ;  $erreur=false ;    
    return response()->json(['Message' => $msg, 'erreur' => $erreur]);

但是,当我在我的javascript文件中使用此代码获得响应时

sendButton.onload = function(e)
{
    Ti.API.debug(this.responseText);

    var json = this.responseText;
    var response = JSON.parse(json);
    if (response.erreur == false)
    {
        alert("a Password has been send to you email ");
    }
    else
    {
        alert(response.Message);
    }
};

我收到此错误

2 个答案:

答案 0 :(得分:0)

错误很简单,响应为空

sendButton.onload = function(e)
{
  Ti.API.debug(this.responseText);

  var json = this.responseText;
  var response = JSON.parse(json);
  if (response !=null && response.erreur == false)
  {
    alert("A password has been sent to your email.");
  }
  else
  {
    console.log(response); //probably doesnt have Message either
  }
};

答案 1 :(得分:0)

@MikeMiller 这是我的Js代码,它与我的API进行通信

loginBtn.addEventListener('click',function(e)
{
    if ( email.value!='')
    {
        try {
        loginReq.open("POST","http://192.168.0.105/appcelerator/public/");//my local ip im testing on my computer 

        var params = {

            email:email.value,

        };
        loginReq.send(params);


       }catch (e)
       {
        alert(e.message);
       }

    }
    else
    {
        alert("All fields are required");
    }
});

现在这里是我的API(php laravel)中的代码

 public function getPassword(Request $request)
        {
            $email = $request["email"];
            $user = \DB::table('users')
                ->where('email', $request['email'])
                ->first();
             $email = $user->email;
            session()->put('email',$email);

            if (!$user)
           {

               $msg = 'invalid email adresses';
               $erreur = true ;
           }else
                    {


                Mail::send('emails.test',['password' => $this->generatePass() ],function($message )
                {
                    $message->to(session()->get('email'),'Bonjour')->subject('welcome:');
                });
             $msg = 'Password has benn send to your email ';
             $erreur = false;
                    }
            return response()->json(['Message' => $msg, 'erreur' => $erreur]);


        }

执行时,我会收到电子邮件地址中的电子邮件,但您知道的回复为空。那是我的问题