-1

I am having one executable URL ..when I hit that URL using GET type request that URL should return me Array in Javascript...

I have created one doGet() in remote server which returns JSON.stringfy(array); I tried this code...can anyone tells me how I can get that array?

fetch(myUrl,{method:'get',headers:{'content-type'-'application/x-www-form-urlencoded'},
mode:'no-cors'}).then(function (response){ console.log(response);
});
GBouffard
  • 1,125
  • 4
  • 11
  • 24
VincentB
  • 13
  • 6

1 Answers1

0

You need CORS permission to read data from a different origin so do not set mode:'no-cors'.

If you are writing an extension page — not a content extension script — such as a background page, popup, or options page then you can request cross-origin permissions:

By adding hosts or host match patterns (or both) to the permissions section of the manifest file, the extension can request access to remote servers outside of its origin.

{
  "name": "My extension",
  ...
  "permissions": [
    "https://www.google.com/"
  ],
  ...
}

Aside: You are making a GET request so you have no request body and so shouldn't describe the type of content in the request body. 'content-type'-'application/x-www-form-urlencoded' is nonsense.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • okay thanks, @Quentin... I am writing this code in content.js for chrome extension ....after removing mode:'no-cors' and 'content-type' - 'application/x-www-form-urlencoded' where can I find that array...do I need to use any other methods to retrieve that array? – VincentB Oct 21 '19 at 14:03
  • Assuming you aren't getting any errors, and assuming that the response is formatted as JSON, then you just need to parse it [as per pretty much any tutorial](https://developers.google.com/web/updates/2015/03/introduction-to-fetch#fetch) – Quentin Oct 21 '19 at 14:05
  • when I am using JSON.parse(response); it is giving me error – VincentB Oct 21 '19 at 14:10
  • @VincentB — Because the response object isn't a string representation of the body. Look at the example I linked to. Also "an error" is unhelpful. Quote error messages. – Quentin Oct 21 '19 at 14:11