arn-arn
arn-arn

Reputation: 1377

ajax call to Spring MVC in a Tomcat Server from ReactJS in an Apache Server

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');
              }
            });
      }

Upvotes: 0

Views: 340

Answers (1)

arn-arn
arn-arn

Reputation: 1377

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>

Upvotes: 1

Related Questions