2017-08-31 84 views
3

這是我第一次使用axios和我遇到了一個錯誤。反應:Axios網絡錯誤

axios.get(
    `http://someurl.com/page1?param1=1&param2=${param2_id}` 
) 
    .then(function(response) { 
    alert(); 
    }) 
    .catch(function(error) { 
    console.log(error); 
    }); 

有了正確的URL和參數,當我檢查網絡請求我確實是從我的服務器得到正確的答案,但是當我打開控制檯我看它沒有調用回調,而是它捕捉到一個錯誤。

Error: Network Error Stack trace: [email protected]http://localhost:3000/static/js/bundle.js:2188:15 [email protected]http://localhost:3000/static/js/bundle.js:1717:14

+1

你可能可以通過'console.log(error.response.data)'來了解更多關於這個的知識。 –

+0

看到這個答案,它會幫助你知道什麼錯誤是完全正確的https://stackoverflow.com/questions/44806333/unable-to-catch-and-log-the-error-response-from-an-axios-request/44806462#44806462 –

+0

@FaustoNA error.response is undefined – Mirakurun

回答

3

如果創建API使用的NodeJS


你快速的應用程序需要使用CORS(跨來源資源共享)。以下內容添加到您的服務器文件:

// This should already be declared in your API file 
var app = express(); 

// ADD THIS 
var cors = require('cors'); 
app.use(cors()); 

對於CORS的更全面的理解,請閱讀Mozilla Documentation on CORS