2016-04-21 71 views
0

我想從跨域圖像得到跨域圖像,我想這通過HTTP調用jQuery中

var img = "http://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg?v=bc7c2f3904bf"; 
    $http.jsonp(img).success(function() { 
        console.log("success"); 

     }).error(function() { 
        console.log("failure"); 
     }); 

我只是想檢查圖像是否存在,如果存在,我想顯示iage,否則我想顯示div而不是img。

,我得到這個錯誤No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

什麼我錯過這裏

任何幫助,可以極大的讚賞提前

+1

這可能不是最好的方式來做到這一點。你能提供更多關於你想要做什麼的細節?你可以創建一個img元素,然後將SRC設置爲你想要的圖像。 –

+0

我只想檢查圖像是否存在,如果存在,我想顯示該圖像,否則我想顯示一個div而不是img。 –

+0

我發佈了一個可能的解決方案 –

回答

0

瞭解什麼是你的目標 謝謝,我認爲你應該做這樣:

var img = document.createElement('img'); 
img.onload = function() { 
    //put Image on dom 
} 

img.onerror = function() { 
    //put div on dom 
} 

希望它有幫助:)