This is possibly a problem with the dailymotion API it appears: see here: jQuery ajax request being block because Cross-Origin
In the above question, the solution is JSONP, however that will not work here for the POST request.
From my test request outside the browser, it appears the server does accept CORS requests, however my Ajax requests consistently fail, no matter how much I fiddle with the options.
My ajax settings:
type:"POST",
//I tried without this parameter
xhrFields: {
//I tried with false
withCredentials: true
},
processData: false,
//I tried with the correct content type of 'multipart/form-data',
contentType: false,
//I tried with this enabled(and without)
//crossDomain:true,
Sample request outside the browser (notice the CORS settings look good)
POST /upload?uuid=21dddfd208868ec5ee38c0f641aa0a43 HTTP/1.1
User-Agent: curl/7.35.0
Host: upload-01 . sv6 . dailymotion . com
Accept: */*
Content-Length: 139
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------5219277ca226e314
* Done waiting for 100-continue
HTTP/1.1 200 OK
Date: Sun, 30 Aug 2015 21:05:54 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
* Server xupload/2.2.6 is not blacklisted
Server: xupload/2.2.6
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description, Session-ID
Access-Control-Allow-Methods: GET, POST
Ajax error:
DAILYMOTION Object { readyState: 0, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 10 more… } error Exception { message: "", result: 2153644038, name: "", filename: "https://ajax.googleapis.com/ajax/li…", lineNumber: 4, columnNumber: 0, inner: null, data: null, stack: ".send@/ajax-googleapis-com/a…" } dmupload.js:98:12
"DAILYMOTION[object Object]error[Exception... "<no message>" nsresult: "0x805e0006 (<unknown>)" location: "JS frame :: ajax-googleapis-com/ajax/libs/jquery/2.1.4/jquery.min.js :: .send :: line 4" data: no]"
firebug error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://upload-01.sv6.dailymotion.com/upload?uuid=6513ac948c1c71afdfca3be8d359d0a5&seal=12dc43863f90d931f63e6492cdb40a9e?access_token=dXgDWFQYX1gPVRxOUBpaEQlFER5ZQ0hHCg. (Reason: CORS request failed).
UPDATE: I got the options request to go through, I chucked jquery and used just ajax, using this code:
var xhr = new XMLHttpRequest();
xhr.open('POST', myUrl2, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
//xhr.setRequestHeader('X-Upload-Content-Length', blob.size);
// xhr.setRequestHeader('X-Upload-Content-Type', blob.type);
xhr.onload = function(e) {
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location');
} else {
alert("err10")
}
};
xhr.onerror = function(e) {
alert("err1", e)
};
xhr.send({'file': blob});
UPDATE 2:
Now I am able to send the POST request, but I am not able to read the response. I thught I would still be able to use the callback feature so that it would not matter, but that did not work. here is an explanatory screenshot and my new code.
var fd4 = new FormData();
console.log(blob)
fd4.append("file", blob);
var xhr = new XMLHttpRequest();
xhr.open('POST', myUrl2, true);
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
//xhr.withCredentials = true;
xhr.setRequestHeader('Content-Length', blob.size);
// xhr.setRequestHeader('X-Upload-Content-Type', blob.type);
xhr.onload = function(e) {
console.log("HEADERS", e.target.getAllResponseHeaders())
if (e.target.status < 400) {
var location = e.target.getResponseHeader('Location');
//sendFile_(blob, blobl.size, blob.type, myUrl2);
} else {
alert("err10")
}
};
xhr.onerror = function(e) {
console.log("HEADERS", e.target.getAllResponseHeaders())
alert("err1", e)
};
xhr.send(fd4);