Angular $ http在应用程序中工作,但REST客户端无法处理相同的数据

时间:2016-07-18 09:59:05

标签: json laravel postman rest-client angular-http

我通过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();
        ...
    }

1 个答案:

答案 0 :(得分:0)

[{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX",}] contact_details为空。

尝试: [{"contact_details":{"name":"Username","email":"username@gmail.com","mobile":"XXXXXXXXXX"}}]