jeudi 22 février 2018

I can't get variables with put method - xmlhttprequest - laravel

I want to save my image data and I'm using XMLHttpRequest, I can send json data as well, I can see them on developer console (in parameters section). But when I can't use them on server side, $request seems null ?

Here is my javascript codes;

 document.querySelectorAll('.galeri-cart').forEach(function (image) {
    image.addEventListener('click',function () {

// update stuff

document.getElementById('submit_button').addEventListener('click',function () {
      let xhr = new XMLHttpRequest(),
          token = document.head.querySelector("[name=csrf-token]").content;
      xhr.open('PUT','images/update/'+id,false);
      xhr.setRequestHeader('X-CSRF-TOKEN',token);
      xhr.onload = function () {
             swal("Updated successfully", {
                  icon: "success",
                    });

                   };
     let data = {};
         data.alt = altInput.value;
         data.url = urlInput.value;

     xhr.send(JSON.stringify(data))
              })
         })
     });

and here is in my controller;

public function updateMedia(Request $request, $id) {
   $image = Media::find($id);
   $image->image_alt_name = $request->input('alt');
   $image->image_path = $request->input('url');
   $image->save();

   return json_endcode($request);
    }

So, do you have any advice ? What is my wrong in here ? And thanks four your help :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire