我正在通过 angular 中的 post 函数发送数据,它工作正常。
$scope.contact_details = [
{"name":"Username",
"email":"username@gmail.com",
"mobile":"XXXXXXXXXX",
}]
var data = {
contact_details : $scope.contact_details
};
var url = API_URL + "/addcontacts?token=" + token ;
$http({
method: 'POST',
url: url,
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(function(response) {
console.log("MyContactDetails : ",response);
}).error(function(response) {
console.log(response);
});
当我使用POSTMAN或RESTClient时,我输入带有令牌的url并选择方法POST。我将以下内容粘贴在正文中
[{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX",}]
这不起作用,我的Laravel函数出错:-
Invalid argument supplied for foreach()
我在控制器中的Laravel功能:-
public function addContact(Request $request){
$contacts = $request->input('contact_details');
$company_id = Auth::guard('api')->user()->company_id;
foreach($contacts as $contact){
$addContact = new ContactList();
...
}
在[{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX",}]
contact_details
为空。
尝试: [{"contact_details":{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX"}}]