2016-04-19 14 views
0

を推奨されていません、私はLaravel 5.2に設定したルートに$ http.post AJAXデータ、すべてが完璧に動作しますが、サーバーが返すこのエラーを送信しようとしています:

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0

これは私のコードです。 PHP:

public function save(Request $request){ 
     $input = $request->all(); 
     try { 
     $direccion = urlencode($input['Calle']." ".$input['Numero'].", ".$input['Ciudad']); 

     $geocode = "https://maps.googleapis.com/maps/api/geocode/json?address=$direccion&key=APIKEY"; 
     $datosGoogle = json_decode($this->curl($geocode), true); 

     $latitud = $datosGoogle['results'][0]['geometry']['location']['lat']; 
     $longitud = $datosGoogle['results'][0]['geometry']['location']['lng']; 

     if(is_double($latitud) && is_double($latitud)){ 
      $input['Latitud'] = $latitud; 
      $input['Longitud'] = $longitud; 
      $new = MyModel::create($input); 
      $data = ["status"=>"ok", "message"=>"Agregado correctamente"]; 
     }else{ 
      $data = ["status"=>"fail", "message"=>"Dirección desconocida, compruebe que los datos son correctos para que podamos agregarla al sistema."]; 
     } 
     } catch (Exception $e) { 
     $data = ["status"=>"error", "message"=>$e->getMessage()]; 
     } 
     return response()->JSON($data); 
    } 

JS:

$scope.registrar = function(form, datos) { 
     $scope.submitted = true; 
     if(form.$valid) { 
     var toSendData = JSON.stringify({ 
      Nombre: datos.nombre, 
      Calle: datos.calle, 
      Numero: datos.numero, 
      Piso: datos.piso, 
      Puerta: datos.puerta, 
      CP: datos.cp, 
      Ciudad: datos.ciudad, 
      Email: datos.email, 
      Necesidades: datos.necesidades, 
      Telefono: datos.telefono 
     }); 
     console.log(toSendData); 
     $http.post($rootScope.recibirNuevoUrl, toSendData).then(function(response){ 
      $scope.hideLoading(); 
      if(response.data.status == "ok"){ 
      $state.go('registro'); 
      }else{ 
      $scope.$parent.showAlert("Error al introducir los datos", response.data.message); 
      } 
     }) 
     }else{ 
     $scope.$parent.showAlert('Validacion de datos', 'El formulario contiene errores. Por favor revise los campos marcados para comprobar los errores.'); 
     } 
    }; 

私はそれを解決することができますか?

答えて

2

私はそれが私のコントローラ内の単一の行を追加して働いてしまった、Laravel 5.2および角度と同じ問題を抱えていた:

editorApp.controller('EditorCtrl', function ($scope, $sce, $http, $location, $interval, $document){ 

    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 

    ... 

私はこの記事では私のためにこの作業を見つけました:https://stackoverflow.com/a/19254137/4021927。正しいとマークされた答えは私のためには機能しませんでした。上記の行は同じ投稿の他の回答からのものです。正解(https://stackoverflow.com/a/20276775/4021927)はあなたがそれを必要とする理由を教えてくれます。

なぜ、この問題が存在するのは、角度(デフォルトで)がJSONのシリアル化を使用してデータを送信するためです:Content-Type: application/jsonとPHPはJSONをネイティブに非直列化しないためです。私はLaravel 5.2に$ GLOBALSを[ 'HTTP_RAW_POST_DATA']の検索と同様の問題を抱えて

{ "foo": "bar", "bar": "foo" } 
+0

私はこの解決策のために何千もの投稿とウェブをレビューしました。ちょっと説明してもらえますか(同じ問題でここに来た新しい人たちを考えて)?百万のおかげで! –

0

:代わりに(JSON)の

foo=bar&bar=foo 

x-www-form-urlencodedにコンテンツタイプを変更することにより、データは次のように送信されます。私は私のコントローラ

0
  • 変更をerror_reportingのため

    1. オープンphp.iniのにphp.iniのerror_reportingの
    2. 検索をオフにした後、作業

      ガット鉱山=>のerror_reportingでRequest::getContent()を使用して問題を解決しました= E_ALL &〜E_DEPRECATED &〜E_STRICT

    3. 終了し、スタート/ Apacheを再起動サーバー
    4. 再起動larave Lサーバ
  • +0

    これは解決策ではなく、実際の問題を隠すだけです。 –

    +0

    あなたは間違いありませんが、推奨される解決策がうまくいかず、これを満たす期限がある場合は、永続的な解決策を探し続けるうちにアプリを稼動させるのに役立ちます。 –

    +0

    提案されたソリューションは私にとって完璧に機能しました –

    0

    laravel 5.2

    /resource/assets/js/bootstrap.js

    window.axios = require('axios'); 
     
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken; 
     
    window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; 
     
    //add default Content-Type 
     
    window.axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

    2データ・マストは、文字列

    const qs = require('qs') 
     
    axios.post('/api/code/create', qs.stringify(this.formItem),) 
     
        .then(function (response) { 
     
         console.log(response) 
     
        }) 
     
        .catch(function (error) { 
     
         console.log(error) 
     
        })
    であります