如何获取Laravel 4中从Angular传递的FormData的值
How to get value of FormData that is passed from Angular in Laravel 4?
我使用angular上传图像(同时上传其他数据),并使用angular服务传递到Laravel的路由,所有这些都使用javascript中的FormData
函数。这是我上传的代码(在coffeescript中):
tag = null
if $('span.tag').length > 0
tag = $('span.tag').map(->
$.trim @innerHTML
).get().join(",")
fd = new FormData()
fd.append "title", $scope.title
fd.append "subtitle", $scope.subtitle
fd.append "date", $scope.date
fd.append "time", $scope.time
fd.append "category", $scope.category
fd.append "content", $scope.content
fd.append "tag", $scope.tag
fd.append "caption", $scope.caption
fd.append "active", !$scope.active
if $scope.file
fd.append "image", $scope.file
alert fd
UploadService.news.save fd
, ( (response) ->
alert response.data
)
我的UploadService
就是这样,指向Laravel的路线:
app.factory "UploadService", ["$resource", ($resource) ->
news: $resource("upload/news")
member: $resource("404/upload/member")
]
我的问题是,我无法读取从FormData
传递到Laravel的数据,即使是这样:
return Response::json(['data' => $_POST['title']]);
或
return Response::json(['data' => Input::get['title']]);
将返回undefined index 'title'
,我的代码的哪一部分是错误的?谢谢你的帮助。
这是我刚刚做的一个测试,目的是检查FormData是否有效:
Route::post('test', function() {
Log::info(Input::all());
return Response::json(['data' => Input::all()]);
});
然后跟踪日志:
php artisan tail
你应该得到这样的东西:
[2014-04-04 20:10:19] server.INFO: {"_token":"MIFCleetMa029UGpUcvXUhawmVdJyl65zYBmXDHM","id":"1""first_name":"Antonio Carlos","last_name":"Ribeiro"}
这是我的(香草)Javascript代码,应该也能在Angular应用程序中正常工作。
var form = document.getElementById('form1');
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://development.consultoriodigital.net/test', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log(xhr.responseText);
}
}
xhr.send(formData);
您还必须将表单放回控制台。
form1
表单可以包含任意数量的表单输入,这无关紧要。
关于这条线的注意事项:
return Response::json(['data' => Input::get['title']]);
应该是
return Response::json(['data' => Input::get('title')]);
相关文章:
- 使用媒体查询和angular更改移动设备中ng href的值
- 在一个控制器中更新服务变量,并在另一个控制器 - Angular JS中使用更新的值
- Angular 2 HTTP显示特定于JSON的值
- 如何使用angular js过滤器格式化数组中的值
- 如何获取Laravel 4中从Angular传递的FormData的值
- 如何访问Angular JS模板指令中设置的值,如“ng class?”
- 我需要一个使用Angular.js的输入框,它的值取决于一个公式
- Angular's ng类未更新,而正在评估的值为
- 为什么 Angular 没有注意到这个变量的值发生了变化
- Angular 插值系统中的点(嵌套)表示法
- 在angular.extend()中向变量返回一个承诺的值
- 从 AJAX 调用返回的值未在 Angular JS 中的控制器中设置为变量
- 获取 Angular JS 中单选按钮中选中的单选按钮的值,该值为 ng-repeat
- 如何在 Angular JS 中检索$scope值定义的函数之外的值
- Element.prop('scrollHeight') 为 Angular.js 中的不同列表项返回相同的值
- 赋值 Angular 2 中 Http 订阅方法返回的值
- 不能将布尔值传递给带有 Angular 的指令
- 如何使用 Angular.JS 在下拉列表中绑定选项的值和文本
- Angular的值在打印时被解析,但不会作为指令属性
- 访问承诺对象angular的值