2016-02-22 2 views
0

Я здесь, в моем PHP файл (Laravel) после того, как я отправить по электронной почте и получить ответ на мобильное приложениеВозвращение JSON ответ от проекта Laravel «Невозможно прочитать Достояние Null»

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

Я получаю эту ошибку

ответ

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

Да, я знаю, что его значение null, но в моем PHP я устанавливаю его так: $ msg = "email sent"; $ erreur = false; return response() -> json (['Message' => $ msg, 'erreur' => $ erreur]); и все еще получается null –

+0

Так что ваш код js должен быть более сильным, чтобы он не выдавал фатальную ошибку. Это ответ на проблему, которую вы опубликовали. Если у вас проблемы с вашим api, это другая проблема. Вы не показываете, где вы разговариваете с API, поэтому не можете помочь дальше –

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


     } 

Когда он выполнен, я получаю электронное письмо в своем адресе электронной почты, но ответ, как вы знаете, имеет значение null. это моя проблема

Смежные вопросы