dqo58772 2016-02-18 19:02
浏览 71
已采纳

如何从角度前端向laravel API发送请求?

I'm building a RESTful API with Laravel 5.2 and I have an AngularJS 1.5 front end. I am successfully writing services to get information but I am having troubble putting or posting anything to the database when I pass it to the API. I've tried doing some searching and but I just don't understand how to actually save data I would send the API. Here is my attempt so far:

-Service from the Factory-

addReceipt: function(request) { 
            return $http.post(api_url + "/rewards/receipts/add", request).then(function(results) {
              console.log(results);
              return results.data;
            });
        }

-From the Controller

$scope.submitReceipt = function() {
                rewardsFactory.addReceipt($scope.model).then(function() {
                    console.log($scope.model);
                    toaster.pop({ type: 'success', title: 'Claim Submitted!', body: "Thanks! We'll take a look at your claim shortly.", showCloseButton: true });
                });
            };

-From Laravel API routes

Route::post('rewards/receipts/add', 'Rewards\RewardsController@addReceipt');

-From Laravel Controller

public function addReceipt(Request $request)
    {
        //Add the Receipt
        DB::table('receipts')->insert(
            ['transaction_id' => $request->input('transactionID'), 
             'client_id' => $request->input('client_id'), 
             'location_id' => $request->input('location_id') ]
        );
    }

My Current Cors setup seems to be working out well enough for at least some traffic so I don't think that is the problem but I'm just still not sure what I'm doing wrong.

  • 写回答

1条回答 默认 最新

  • dragon201401 2016-02-18 20:33
    关注

    Note that $http does not send form encoded data by default, it sends application/json in request body.

    I don't do any work with laravel but if you check $_POST you will see it is empty so $request->input is probably empty also.

    In php you can access the response body using :

    json_decode(file_get_contents('php://input')[,true/*optional to convert to array*/])
    

    I believe that json_decode($request->getContent()) will do the same in laravel

    The alternative is to use the following $http set up taken from the docs to send form encoded data

    .controller(function($http, $httpParamSerializerJQLike) {
      //...
    
      $http({
        url: myUrl,
        method: 'POST',
        data: $httpParamSerializerJQLike(myData),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      });
    
    });
    

    You can also set $http.defaults in a run block so all post or put are sent as x-www-form-urlencoded and not have to add the config to each use

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable