1

I am trying to get a json response from an API request. I am using jetty servlet, and my GET request works when I try it in post man.

However I am trying to create a website that will query my API but I get the error:

Failed to load localhost:49000/api/coin/help: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

My js code looks like:

$(function () {

    $.ajax({

        type: 'GET',
        url: 'localhost:49000/api/coin/help',
        success: function(data) {
            console.log('success', data);
        }
    });
});

Can someone please tell me how I can get a similar response as if I was using postman. I have been stuck on this for days and finally had to ask here, I know there are similar questions/answers however it has not helped me.

XML file:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.mycompany.jerseytutorial</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>
fints1col
  • 167
  • 1
  • 2
  • 8

2 Answers2

0

Add @CrossOrigin annotation in your rest api at your method which has url: /api/coin/help (if you are using Spring) If EE look at this post: [How to enable Cross domain requests on JAX-RS web services?

Moler
  • 955
  • 2
  • 8
  • 17
  • thank you. I looked at that posed and this worked: return Response.ok(resp).header("Access-Control-Allow-Origin", "*").build(); – fints1col Jul 11 '18 at 12:17
  • This was working for weeks. However now I get the same: Failed to load http://localhost:49000/.... : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404. – fints1col Jul 26 '18 at 10:20
0

Change localhost:49000/api/coin/help to http://localhost:49000/api/coin/help

peeebeee
  • 2,541
  • 6
  • 21
  • 26
  • that got me one step forward to a different error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. I have no idea what happend, it was showing the expected JSON response for about 10 minutes, now I am getting the above error – fints1col Jul 11 '18 at 12:08
  • See @Moler's answer on that one. – peeebeee Jul 11 '18 at 12:10