2016-04-29 81 views
0

我能夠通過郵遞員,甚至通過我的應用程序時,我用這樣簡單的JavaScript請求來發布基地64編碼的圖像:NG-文件上傳跨域錯誤

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); 

但是,當我使用簡單

<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> 

腳本:

代碼 ng-file-upload

HTML複製210

我得到一個跨域錯誤:

XMLHttpRequest cannot load 'url'. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

基本上我試圖從開始實際場景的簡化版本到最基本的使用情況下,網站上的所有不同的組合。它只是我或角度錯誤真的令人沮喪,因爲它們之間有很多層。 C#絕對不是那麼難

回答

1

好吧算了一下。在上傳功能中,我使用的是整個網址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); 
    }); 
};