0

I am using fetch() to grab some data from my server. I have done the exact same thing many times before but now my browser is giving me the following error: Uncaught (in promise) SyntaxError: Unexpected end of input. this happens after:

getCategories()
.then((j)=> {    
    return j.json();
})

the error is thrown in response to j.json(), the getCategories function is :

export function getCategories() {
    var req = new window.Request(apiUrl + "/GetAllCategories",{
        method: "GET",
        mode: 'no-cors',
        dataType: 'json',
        headers: {
            'Accept': 'application/json'
        }   
    });
    return fetch(req);    
}

the response from the server: [{"CategoryId":6,"Name":"Eten"}]

with headers:

Request URL:http://localhost:60249/api/app/GetAllCategories
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:60249
Response Headers
view source
Cache-Control:no-cache
Content-Length:32
Content-Type:application/json; charset=utf-8
Date:Sun, 18 Dec 2016 21:20:01 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?    YzpcdXNlcnNcaWRzIHZhbiBkZXIgemVlXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTdcUHJvamVjdHNcdWl0Z2F2ZSBzZXJ2ZXJcV2ViQXBwbGljYXRpb24xXGFwaVxhcHBcR2V0QWxsQ2F0ZWdvcmllcw==?=
Request Headers
view source
accept:application/json
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:60249
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36`
Marc
  • 3,905
  • 4
  • 21
  • 37
Ids van der Zee
  • 814
  • 1
  • 13
  • 22
  • 4
    `mode: 'no-cors'` guarantees no access to response - which would explain unexpected end of input, as there is no input - contrary to some peoples popular belief (or hope) `mode: no-cors` does not bypass CORS security – Jaromanda X Dec 18 '16 at 21:58
  • Thanks for the response, I did not know that. I fixed the problem by adding [EnableCors(origins: "http://localhost:3000", headers: "*", methods: "*")] to my controller methods and removind the mode: no-cors from the request. – Ids van der Zee Dec 19 '16 at 17:43

0 Answers0