0

I am trying to upload a photo from JavaScript using the StreetView Publish API and it seems that everything i try fails ... currently i have the uploadUrl and i need to make a post request with the actual image data

this is that i ended up doing

var input = document.querySelector('input[type="file"]').files;
var reader = new FileReader();
reader.onload = function(){
      var dataURL = reader.result;
      var xhr = new XMLHttpRequest();
      xhr.open("POST", window.uploadUrl, true);
      xhr.setRequestHeader("Authorization", gapi.client.getToken().token_type + ' ' + gapi.client.getToken().access_token);
      xhr.setRequestHeader("Content-Type", "image/jpeg");
      xhr.setRequestHeader("X-Goog-Upload-Protocol", "raw");
      xhr.setRequestHeader("X-Goog-Upload-Content-Length", dataURL.length );
      xhr.onreadystatechange = function() {//Call a function when the state changes.
        if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
            console.log(xhr);
        }
      }
      xhr.send(dataURL);
    };
reader.readAsDataURL(input[0]);

the answer i get is the following one: Failed to load

https://streetviewpublish.googleapis.com/media/user/.../photo/...: Response for preflight is invalid (redirect)

can anyone suggest any possible solution to this?

thanks

UPDATE from what i see ... when i am trying to upload the image, 2 requests are generated ... both are OPTIONS and 302 status and none of them have the headers i am trying to send ... mainly the access token

var xhr = new XMLHttpRequest();
xhr.open("POST", window.uploadUrl + "?key=...", true);
xhr.setRequestHeader("Authorization", gapi.client.getToken().token_type + ' ' + gapi.client.getToken().access_token);
xhr.setRequestHeader("Authorization", gapi.client.getToken().access_token);
xhr.setRequestHeader("Content-Type", 'image/jpeg');
xhr.setRequestHeader("X-Goog-Upload-Protocol", "raw");
xhr.setRequestHeader("X-Goog-Upload-Content-Length", dataURL.length );
xhr.onreadystatechange = function() {
   if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
      console.log(xhr);
   }
}
xhr.send(dataURL);
Wowzaaa
  • 3,730
  • 4
  • 21
  • 23
  • You may refer with this [thread](https://stackoverflow.com/questions/33645511/why-my-ajax-showing-preflight-is-invalid-redirect-error). You might encounter the error `Response for preflight is invalid (redirect)` when trying to call https web service as http webservice. It was stated that this error is produced because of redirection status 302 when you try to call http instead of https. You may also check this SO post: https://stackoverflow.com/questions/45284051/response-for-preflight-is-invalid-redirect-in-angular2-4 – abielita Nov 23 '17 at 16:33
  • @abielita thanks for trying to help :) ... tried it from https ... same thing :( could the fact that i am missing some XMP tags ... trigger this error? i mean for those cases i am expecting more specific errors :) – Wowzaaa Nov 23 '17 at 20:12

0 Answers0