duanhong1985 2017-11-06 19:23
浏览 41
已采纳

从AngularJS到Laravel的Http Post

I have been doing research and nothing I have found has made any sense to me. I am totally new to laravel and I am wanting to use it as a backend to my AngularJS. I have good experience with angular so here is my controller function making the http call:

var jsonData = {
  text: "test"
};
$http.post("http://127.0.0.1:8000/submit", {
  data: jsonData
}).then(function(response) {
  console.log("SUCCESS");
})

And then here is my route in file api.php:

Route::post('/submit', function(Request $request) {
  $data = $request->input('data');
  return $data;
});

So then I run php artisan serve and the console says Laravel development server started: <http://127.0.0.1:8000>. I try to reach the server via angular and nothing happens. I was wondering if someone can help me figure out what I am doing wrong? How can I post my data from angular to laravel and then send it back to my angular?

</div>
  • 写回答

1条回答 默认 最新

  • dougu3290 2017-11-06 20:40
    关注

    So this came down to a few issues:

    1) Chrome does not like pages to be accessed via the file. It needs to run within a webserver. Luckily, Laravel works well with angularjs when the angularjs files are placed inside of the public/ folder.

    2) Since the route was placed into api.php, the actual route to the file was api/submit.

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

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型