Ajax 使用';时Laravel 4未接收角度$http PUT数据;多部分/表单数据';

Ajax 使用';时Laravel 4未接收角度$http PUT数据;多部分/表单数据';,ajax,rest,angularjs,laravel-4,Ajax,Rest,Angularjs,Laravel 4,哈罗:)我正在使用一个内置于Laravel4的RESTfulAPI,结合Angular在前端进行重载 其目的是通过将表单数据发布到API(包括文件),在数据库中创建新的“项”。用户还可以使用PUT/PATCH以相同的方式编辑项目 无论出于何种原因,我都可以发布数据(使用$http),这很好,但如果我使用PUT,Laravel不会收到任何数据。我也试过这个补丁。数据肯定是通过$http发送的,您可以在这里看到: 我可以通过回显$input变量看出Laravel没有获取/处理任何数据 我不确定这是A

哈罗:)我正在使用一个内置于Laravel4的RESTfulAPI,结合Angular在前端进行重载

其目的是通过将表单数据发布到API(包括文件),在数据库中创建新的“项”。用户还可以使用PUT/PATCH以相同的方式编辑项目

无论出于何种原因,我都可以发布数据(使用$http),这很好,但如果我使用PUT,Laravel不会收到任何数据。我也试过这个补丁。数据肯定是通过$http发送的,您可以在这里看到:

我可以通过回显$input变量看出Laravel没有获取/处理任何数据

我不确定这是Angular未以正确方式发送数据的问题,还是Laravel未正确接收/处理数据的问题

Javascript(稍微简化):

var formdata = new FormData();
// Unfortunately we cant just walk through the data with a FOR because the data is an object, not an array – We have to be explicit
// If data exists THEN add data to FormData ELSE do nothing
formdata.append('title', $scope.item.title);
formdata.append('description', $scope.item.description);
formdata.append('image', $scope.item.image);
formdata.append('tags', $scope.item.tags);
formdata.append('priority', $scope.item.priority);


edititem: function(formdata) {
    // Edits a particular list
    // id: the ID of the list to edit
    // data: the edited list object
    var promise = $http({
            method: 'PUT',
            url: 'http://mamp.local/api/v1/items/64',
            headers: { 'Content-Type': undefined },
            data: formdata,
            transformRequest: angular.identity
        })
        .error(function(data, status, headers, config) {
            debug(data, 'API FAIL - edit item');
            return data;
        })
        .success(function(response){
            debug(response.data, 'API Success - edit item');
            return response.data;
        });

    return promise;
},
/**
* Update the specified resource in storage.
*
* @param  int  $id
* @return Response
*/
public function update($id)
{
    // Try and store the new List
    $item = $this->itemRepo->updateitem($id, $this->user, Input::all());

    // Lets check if we have any validation errors
    if (count($item->errors()))
    {
        return Response::json(['errors' => $item->errors()->all()], 400);
    }
    // No errors
    else
    {
        return Response::json(['id' => $item->id], 200);
    }
}


/**
* Updates the item
*
* @param int $id the item ID
* @param User $user
* @param array $input
*
* @return item the item
*/
public function updateitem($id, User $user, $input)
{
    // Grab the item
    $item = $this->finditem($id, $user);

    // Fill item with new input
    $item->fill($input);

    // Do we have an image?
    if (Input::hasFile('image'))
    {
        // Handle resizing of the image
        $item->image = $this->imageManipulator->resize($this->imageSizes, $this->imageDir, Input::file('image'));
    }

    // Try and save the item
    if ($item->save())
    {
        // item saved, save the tags
        $this->tagRepo->saveTags($input['tags'], $item, $user);
    }

    // Return the item
    return $item;
}
PHP:

var formdata = new FormData();
// Unfortunately we cant just walk through the data with a FOR because the data is an object, not an array – We have to be explicit
// If data exists THEN add data to FormData ELSE do nothing
formdata.append('title', $scope.item.title);
formdata.append('description', $scope.item.description);
formdata.append('image', $scope.item.image);
formdata.append('tags', $scope.item.tags);
formdata.append('priority', $scope.item.priority);


edititem: function(formdata) {
    // Edits a particular list
    // id: the ID of the list to edit
    // data: the edited list object
    var promise = $http({
            method: 'PUT',
            url: 'http://mamp.local/api/v1/items/64',
            headers: { 'Content-Type': undefined },
            data: formdata,
            transformRequest: angular.identity
        })
        .error(function(data, status, headers, config) {
            debug(data, 'API FAIL - edit item');
            return data;
        })
        .success(function(response){
            debug(response.data, 'API Success - edit item');
            return response.data;
        });

    return promise;
},
/**
* Update the specified resource in storage.
*
* @param  int  $id
* @return Response
*/
public function update($id)
{
    // Try and store the new List
    $item = $this->itemRepo->updateitem($id, $this->user, Input::all());

    // Lets check if we have any validation errors
    if (count($item->errors()))
    {
        return Response::json(['errors' => $item->errors()->all()], 400);
    }
    // No errors
    else
    {
        return Response::json(['id' => $item->id], 200);
    }
}


/**
* Updates the item
*
* @param int $id the item ID
* @param User $user
* @param array $input
*
* @return item the item
*/
public function updateitem($id, User $user, $input)
{
    // Grab the item
    $item = $this->finditem($id, $user);

    // Fill item with new input
    $item->fill($input);

    // Do we have an image?
    if (Input::hasFile('image'))
    {
        // Handle resizing of the image
        $item->image = $this->imageManipulator->resize($this->imageSizes, $this->imageDir, Input::file('image'));
    }

    // Try and save the item
    if ($item->save())
    {
        // item saved, save the tags
        $this->tagRepo->saveTags($input['tags'], $item, $user);
    }

    // Return the item
    return $item;
}
我希望这是足够的信息,让我知道如果需要澄清什么


范库!:)

为什么这样做:
标题:{“内容类型”:未定义}

它应该是
标题:{'Content Type':'application/json'}


如果Laravel没有将内容类型视为application/json,它将无法正确获取您的json帖子。

您能断言Request::isJson()为false吗?缺少输入的最常见原因是JSON输入覆盖了POST数据,反之亦然。我没有太多时间深入研究这个问题,但您可以使用rest客户端来测试问题是在客户端还是服务器端。顺便说一句,你的粘贴链接坏了。这是因为他正在用
boundary
尝试
multipart/formdata
。没有别的了。