[英]ng-file-upload cross domain error
當我使用像這樣的簡單javascript請求時,我可以通過郵遞員甚至通過我的應用程序發布基於base 64編碼的圖像:
var data= "userPhoto=data%3Aimage%2Fjpeg%3Bbase64%2C%2F9j%2F4AAQSkZJRgABAQEAS ................(image data) Rd1bbfc2B%2FdDXGWxWHtA2N6NrL%2BLNf6wIa92a5m5v2s0c7tObhgdJMdV6Rm4mt7YkqukTMsv2vVsgl5j73tQasFOHYMP3nqf%2F%2FZ";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8000/test2");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "b98cbdf1-8918-b8fb-6b50- a626c301cffc");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.send(data);
但是當我使用從ng-file-upload復制的簡單代碼時
HTML:
<div class="button" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'" ngf-accept="'image/*'" ngf-max-size="20MB" ngf-min-height="100" ngf-resize="{width: 100, height: 100}">Select</div>
腳本:
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) {
// upload later on form submit or something similar
$scope.submit = function() {
if ($scope.form.file.$valid && $scope.file) {
$scope.upload($scope.file);
}
};
我收到跨域錯誤:
XMLHttpRequest無法加載“ URL”。 跨源請求僅支持以下協議方案:http,數據,chrome,chrome擴展名,https,chrome-extension-resource。
基本上,我嘗試了從實際方案到簡化版本到站點上最基本用例的所有不同組合。 由於介於兩者之間的層數眾多,因此我或角度誤差確實令人沮喪。 C#絕對不是那么難
好,知道了。 在上載功能中,我使用了整個URL http://localhost:8000/test1
,但是我應該只使用/test1
$scope.upload = function (file) {
Upload.upload({
url: '/test1',
data: {'userPhoto':file}
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.