简体   繁体   中英

angular laravel nginx 400 Bad Request

Help, I've got 400 error on POST and or PUT method, but GET works just fine, I'm using angular as front end and laravel as API, my server is using nginx, I've used CORS and I everything works fine on my local vagrant which is running on apache.

I'm sure I have my route set correctly, here's some of it from the module I use:

 Route::group(array('prefix'=>'/api', 'middleware' => 'cors'),function(){
     Route::post('/create_level',      'LevelController@store');
     Route::get('/read_level',        'LevelController@index');
     Route::get('/read_level/{id}',    'LevelController@show');
     Route::put('/read_level/{id}', 'LevelController@update');
     Route::delete('/read_level/{id}', 'LevelController@destroy');

here's part of my angular service:

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;        
    }
....

oh I forgot to mention different method cause different error code POST cause 405, PUT cause 400, and I've tried using Postman: POST is working using text type and return 405 using application/json, but when I tried PUT method even though it return 200 I only got NULL data entered to my db (text type), and if I use application/json it return 400

Please Help

Finally found solution: change $http.post to:

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

somehow it works, exept on my login page which using stellizer to do post method and i can't find how should I change it without breaking all the function...

any one? I only need to add: headers: {'Content-Type': 'application/x-www-form-urlencoded'} and data: $.param({ ...... })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM