我通过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();
...
}
答案 0 :(得分:0)
[{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX",}]
contact_details
为空。
尝试:
[{"contact_details":{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX"}}]