gw1500se Posted August 20, 2022 Share Posted August 20, 2022 I can't find this documented any where but what responses are available other than json? I have this function: function requests(url) { return fetch(url).catch(function(error) { return(null); }) .then(function(response) { if (response==null) { return(null); } else if (!response.ok) { console.log("caught error: "+response.status); } try { return response.json(); } catch(e) { return resonse.html(); } }); } The response can be either json or html. When html results I get an error: VM13:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON How do I tell the difference since try/catch doesn't work? TIA. Quote Link to comment https://forums.phpfreaks.com/topic/315216-types-of-fetch-responses/ Share on other sites More sharing options...
Barand Posted August 20, 2022 Share Posted August 20, 2022 The ones I'm aware of (for ajax anyway) are JSON TEXT HTML Quote Link to comment https://forums.phpfreaks.com/topic/315216-types-of-fetch-responses/#findComment-1599637 Share on other sites More sharing options...
requinix Posted August 20, 2022 Share Posted August 20, 2022 55 minutes ago, gw1500se said: I can't find this documented any where Really? https://developer.mozilla.org/en-US/docs/Web/API/Fetch_APIhttps://developer.mozilla.org/en-US/docs/Web/API/Response Quote Link to comment https://forums.phpfreaks.com/topic/315216-types-of-fetch-responses/#findComment-1599640 Share on other sites More sharing options...
kicken Posted August 20, 2022 Share Posted August 20, 2022 3 hours ago, gw1500se said: what responses are available other than json? The response data could be anything. JSON is common so there's a method on the response object to parse it, but you could get whatever data you wanted as either text or binary data. You should be looking at the Content-type header to find out what the type is and react accordingly. 3 hours ago, gw1500se said: since try/catch doesn't work? Because those functions immediately return a promise, which is an action which won't fail. The failure comes later and the promise is rejected, which you handle with .catch() as described in your other thread. Quote Link to comment https://forums.phpfreaks.com/topic/315216-types-of-fetch-responses/#findComment-1599644 Share on other sites More sharing options...
gw1500se Posted August 21, 2022 Author Share Posted August 21, 2022 (edited) 17 hours ago, requinix said: Really? https://developer.mozilla.org/en-US/docs/Web/API/Fetch_APIhttps://developer.mozilla.org/en-US/docs/Web/API/Response Yes, really. Those never showed up in any of my searches. Thanks for the links. Edited August 21, 2022 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/315216-types-of-fetch-responses/#findComment-1599652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.