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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图