2016-01-04 67 views
1

帮助,我有POST和或PUT方法400错误,但GET工作得很好, 我使用angular作为前端和laravel作为API,我的服务器使用nginx, 我已经使用CORS和我一切工作正常我的当地流浪者是在Apache上运行。角laravel nginx 400错误的请求

我敢肯定,我有我的路线设置正确,这里是它的一些我用的模块:

Route::group(array('prefix'=>'/api', 'middleware' => 'cors'),function(){ 
    Route::post('/create_level',  '[email protected]'); 
    Route::get('/read_level',  '[email protected]'); 
    Route::get('/read_level/{id}', '[email protected]'); 
    Route::put('/read_level/{id}', '[email protected]'); 
    Route::delete('/read_level/{id}', '[email protected]'); 

这里是我的角度服务的一部分:

app.service("edulevelService", function ($http, $q, $rootScope) 
{ 
edu.updateEdulevel = function(id, edu){ 
      var deferred = $q.defer(); 
      $http.put($rootScope.endPoint + 'read_level/'+ id, edu) 
      .success(function(res) 
       { 
       deferred.resolve(res); 
       }) 
      .error(function(err, stat){ 
       deferred.reject(err); 
       console.log('error code: Ser-UEDU'); 
       });   
       return deferred.promise; 
     } 

edu.createEdulevel = function(edu){ 
     var deferred = $q.defer(); 
     $http.post($rootScope.endPoint + 'create_level', edu) 
     .success(function(res) 
      { 
      deferred.resolve(res); 
      }) 
     .error(function(err, stat){ 
      deferred.reject(err); 
      console.log('error code: Ser-CEDU'); 
      }); 
     return deferred.promise;   
    } 
.... 

哦,我忘了提到不同的方法导致不同的错误代码POST原因405,PUT导致400,并且我尝试使用邮差: POST正在使用文本类型并返回405使用应用程序/ json, 但是当我尝试 PUT方法甚至虽然它返回200我只得到NULL数据进入到我的分贝(文本类型),如果我使用的应用程序/ JSON其返回400

请帮助

+0

错误信息除坏请求外还有什么? – ssuhat

+0

没有,哦,我忘了提及不同的方法导致不同的错误代码POST导致405,PUT导致400,我试过使用函数回声“...”;在PHP控制器中,它没有被调用,但它在router.php上被调用 – Michael

回答

0

终于找到了解决办法: 变化$ http.post到:

$http({ 
 
\t \t method: "post", 
 
\t \t url: $rootScope.endPoint + 'create_level', 
 
\t \t headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
 
\t \t data: $.param({ .... }) 
 
\t })

不知它的工作原理,exept其使用stellizer做POST方法我的登录页面上,我无法找到我应该怎么改变它没有打破所有的功能...

任何一个? 我只需要添加: 头:{ '内容类型': '应用程序/ X WWW的形式,进行了urlencoded'} 和 数据:$ .PARAM({......})