0

I am trying to call a controller method in a spring mvc application that is in the tomcat server using ajax in my ReactJS page which is in the Apache Server. In the tomcat console, I can see that spring is receiving the call and processing it. However, the ajax call is not calling the "success" function even though the spring application has processed the call successfully. Is there any other configuration needed in my ReactJS ajax code?

componentDidMount: function() {
        $.ajax({
              type: "GET",
              url: "http://localhost:8080/SpringApp/ajaxCallTest",
              data: {testId:'345'},
              dataType: 'json',
              success: function(response) {
            alert('in success');
              }
            });
      }
arn-arn
  • 1,328
  • 1
  • 21
  • 31
  • Nothing on javascript console? – jmartins May 04 '15 at 20:00
  • Try to create an error function callback like `error: function(xhr, status, err) {console.error('error', status, err.toString()); }` and see if sth is shown on browser js console. – jmartins May 04 '15 at 20:02
  • 1
    thanks jmartins for your comments. I did what you suggested and this is what I got from the javascript console: XMLHttpRequest cannot load ... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. – arn-arn May 04 '15 at 20:23
  • Are setting the header in the response in your server? Something like `response.setContentType("application/json"); ` – jmartins May 05 '15 at 11:46
  • Could you post your code in the server? – jmartins May 05 '15 at 11:47
  • the application i'm calling is using spring mvc and it is returning a json result. here is the code: @RequestMapping(value="/ajaxCallTest", method = RequestMethod.GET, produces="application/json") @ResponseBody public List getTestData(int testId){ return getDataService.getActualData(testId); } – arn-arn May 05 '15 at 12:19
  • Try something like `@RequestMapping(value="/ajaxCallTest", method = RequestMethod.GET, produces="application/json", headers="content-type=application/json") ` – jmartins May 05 '15 at 12:23
  • 1
    thanks for the suggestion. I appreciate your help. However, this didn't work either. I have found the solution which is to import some jar files. here is the similar thread and the answer i have chosen was the CORS filter. http://stackoverflow.com/questions/27056672/how-to-integrate-cross-origin-resource-sharing-with-spring-mvc-4-0-0-restful-web – arn-arn May 05 '15 at 14:37

1 Answers1

1

add this to the web.xml:

<filter>
        <!-- The CORS filter with parameters -->
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

        <!-- Note: All parameters are options, if omitted the CORS 
             Filter will fall back to the respective default values.
          -->
        <init-param>
            <param-name>cors.allowGenericHttpRequests</param-name>
            <param-value>true</param-value>
        </init-param>

        <init-param>
            <param-name>cors.allowOrigin</param-name>
            <param-value>*</param-value>
        </init-param>

        <init-param>
            <param-name>cors.allowSubdomains</param-name>
            <param-value>false</param-value>
        </init-param>

        <init-param>
            <param-name>cors.supportedMethods</param-name>
            <param-value>GET, HEAD, POST, OPTIONS</param-value>
        </init-param>

        <init-param>
            <param-name>cors.supportedHeaders</param-name>
            <param-value>*</param-value>
        </init-param>

        <init-param>
            <param-name>cors.exposedHeaders</param-name>
            <param-value>X-Test-1, X-Test-2</param-value>
        </init-param>

        <init-param>
            <param-name>cors.supportsCredentials</param-name>
            <param-value>true</param-value>
        </init-param>

        <init-param>
            <param-name>cors.maxAge</param-name>
            <param-value>3600</param-value>
        </init-param>

    </filter>
arn-arn
  • 1,328
  • 1
  • 21
  • 31