2016-04-25 15 views
17

मैं एक/redux आवेदन प्रतिक्रिया और मैं एक Sever के लिए एक सरल GET अनुरोध करने के लिए कोशिश कर रहा हूँ है देता है:लाने प्रतिक्रिया एक खाली प्रतिक्रिया शरीर

fetch('http://example.com/api/node', { 
    mode: "no-cors", 
    method: "GET", 
    headers: { 
    "Accept": "application/json" 
    } 
}).then((response) => { 
    console.log(response.body); // null 
    return dispatch({ 
    type: "GET_CALL", 
    response: response 
    }); 
}) 
.catch(error => { console.log('request failed', error); }); 

समस्या यह है कि प्रतिक्रिया शरीर में खाली है .then() फ़ंक्शन और मुझे यकीन नहीं है कि क्यों। मैंने ऑनलाइन उदाहरणों की जांच की और ऐसा लगता है कि मेरे कोड को काम करना चाहिए, इसलिए मुझे यहां कुछ याद आ रही है। बात यह है कि, यदि मैं क्रोम के देव उपकरण में नेटवर्क टैब की जांच करता हूं, तो अनुरोध किया जाता है और मुझे वह डेटा प्राप्त होता है जिसे मैं ढूंढ रहा हूं।

क्या कोई इस पर प्रकाश डाल सकता है?

संपादित करें:

मैं प्रतिक्रिया परिवर्तित करने की कोशिश की।

.text() का उपयोग कर:

fetch('http://example.com/api/node', { 
    mode: "no-cors", 
    method: "GET", 
    headers: { 
    "Accept": "application/json" 
    } 
}) 
.then(response => response.text()) 
.then((response) => { 
    console.log(response); // returns empty string 
    return dispatch({ 
    type: "GET_CALL", 
    response: response 
    }); 
}) 
.catch(error => { console.log('request failed', error); }); 

और .json() साथ:

fetch('http://example.com/api/node', { 
    mode: "no-cors", 
    method: "GET", 
    headers: { 
    "Accept": "application/json" 
    } 
}) 
.then(response => response.json()) 
.then((response) => { 
    console.log(response.body); 
    return dispatch({ 
    type: "GET_CALL", 
    response: response.body 
    }); 
}) 
.catch(error => { console.log('request failed', error); }); // Syntax error: unexpected end of input 

क्रोम देव उपकरणों में देख रहे हैं:

enter image description here

+0

मुझे काम करने के लिए 'fetch' नहीं मिल सका इसलिए मैंने [redux-api-middleware] (https://github.com/agraboso/redux-api-middleware/) का उपयोग शुरू किया। जब आप अनुरोध करते हैं तो आपको जेसन को प्रतिक्रिया को उसी तरह से परिवर्तित करना होगा जैसा कि यहां लोगों ने सुझाव दिया: –

उत्तर

41

मैं सिर्फ इस में भाग गया। जैसा कि answer में बताया गया है, mode: "no-cors" का उपयोग करके आपको opaque response मिल जाएगा, जो शरीर में डेटा वापस नहीं प्रतीत होता है।

अपारदर्शी: क्रॉस-मूल संसाधन के लिए "नो-कॉर्स" अनुरोध के लिए प्रतिक्रिया। Severely restricted

मेरे मामले में मैं Express का उपयोग कर रहा था। cors for Express स्थापित करने के बाद और इसे कॉन्फ़िगर किया गया और mode: "no-cors" हटा दिया गया, मुझे एक वादा वापस कर दिया गया। प्रतिक्रिया डेटा वादे में होगा, उदाहरण के लिए

fetch('http://example.com/api/node', { 
    // mode: 'no-cors', 
    method: 'GET', 
    headers: { 
    Accept: 'application/json', 
    }, 
}, 
).then(response => { 
    if (response.ok) { 
    response.json().then(json => { 
     console.log(json); 
    }); 
    } 
}); 
2

आप प्रतिक्रिया के शरीर अवश्य पढ़ें:

+०१२३५१६४१०६१
fetch(url) 
    .then(res => res.text()) // Read the body as a string 

fetch(url) 
    .then(res => res.json()) // Read the body as JSON payload 

एक बार जब आप शरीर पढ़ा है आप इसे हेरफेर करने में सक्षम हो जाएगा:

fetch('http://example.com/api/node', { 
    mode: "no-cors", 
    method: "GET", 
    headers: { 
    "Accept": "application/json" 
    } 
}) 
    .then(response => response.json()) 
    .then(response => { 
    return dispatch({ 
     type: "GET_CALL", 
     response: response 
    }); 
    }) 
+0

उत्तर के लिए धन्यवाद! मैंने कोशिश की, लेकिन मुझे अभी भी सही प्रतिक्रिया मिल सकती है। संपादित प्रश्न देखें। –

+0

@RazvanIlin '.text()' – Florent

+0

के बजाय '.json()' का उपयोग करें क्षमा करें, मुझे अभी एहसास हुआ कि मैंने पिछले उदाहरण में गलत स्निपेट की प्रतिलिपि बनाई है। मैं वास्तव में वहां 'जेसन()' का उपयोग कर रहा था, लेकिन मुझे सिंटेक्स त्रुटि मिलती है, जो मुझे लगता है कि निकाल दिया जाता है क्योंकि यह जेसन –

9

आप json करने के लिए अपने response कन्वर्ट करने के लिए इससे पहले कि आप response.body

से उपयोग कर सकते हैं की आवश्यकता होगी docs

fetch(url) 
    .then(response => response.json()) 
    .then(json => { 
    console.log('parsed json', json) // access json.body here 
    }) 
2

response.json() उपयोग करने के लिए प्रयास करें:

fetch('http://example.com/api/node', { 
    mode: "no-cors", 
    method: "GET", 
    headers: { 
    "Accept": "application/json" 
    } 
}).then((response) => { 
    console.log(response.json()); // null 
    return dispatch({ 
    type: "GET_CALL", 
    response: response.json() 
    }); 
}) 
.catch(error => { console.log('request failed', error); }); 
1
fetch("http://localhost:8988/api", { 
     //mode: "no-cors", 
     method: "GET", 
     headers: { 
      "Accept": "application/json" 
     } 
    }) 
    .then(response => { 
     return response.json(); 
    }) 
    .then(data => { 
     return data; 
    }) 
    .catch(error => { 
     return error; 
    }); 

यह मेरे लिए काम करता है।

संबंधित मुद्दे