26 September 2016

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data with REST API Call

Hi friends,

Today I was trying to call my REST API which was on (localhost:9001) from an application from (localhost:8080) through javascript.

The status from rest call was 200 OK but I was unable to see the data. SO I checked the issue in mozilla firefox Network tab as shown below:



So I can see that there was an error as "SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data".

So in order to identify the exact issue I have logged the error in the console by specifying

catch((error) => { console.log(error); }); 

So I can see the following error in the console:

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9001/TestRestEasy/rest/departments/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."


 This error coming because REST service is available at 9001 port and we are trying to invoke it from 8089 port.

So in order to rectify this issue you need to write down a response filter for your rest api.

package com.test.service;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

/**
 *
 * @author kunalk
 */
public class DepartmentRestFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
     response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHttpHeaders().add("Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization");
        response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
        response.getHttpHeaders().add("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS, HEAD");

        return response;
    }
}


 To register this filter in your application you need to add the following entry in the web.xml file:

  <servlet>
    <servlet-name>JerseyRESTService</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.test.service</param-value>
    </init-param>
    <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
      <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>com.test.service.DepartmentRestFilter</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JerseyRESTService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>


Now when you  run your application again, you will not get this error anymore.

Happy Coding :)









0 on: "SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data with REST API Call"