无法在Google应用引擎中处理跨域json请求并在json中获得响应

时间:2014-10-14 07:31:36

标签: json spring google-app-engine spring-mvc cors

XMLHttpRequest无法加载http://XXXXXXXX.com/getProduct.html。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://example.com'因此不允许访问。

我的客户端jsp代码:

<script type="text/javascript" src="Jquery.js"></script>

<script>
function submitLogin() {
        var obj = {
                "productMasterId" : "1"
        };

        $.ajax({ 
            url: "http://XXXXXXXX.com/getProduct.html", 
            type: 'POST',
            contentType: 'text/javascript; charset=utf-8',
            crossDomain : true,
            mimeType: 'text/javascript',
            success : function(response) {
                alert("done");
                alert(response);
            }
            ,
            error : function(response) {

            }
        }); 

    }
</script>
<a href="javascript:submitLogin()">click </a>

我的服务器端代码:

@RequestMapping("/getProductDetailsForReview.html")
    public @ResponseBody PaymentForm getProductDetailsForReview(@RequestBody PaymentForm paymentForm, HttpServletResponse response) {
        log.debug("Start of method getProductDetailsForReview");
        PaymentForm form = userPaymentService.getProductForReview(paymentForm);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        log.debug("End of method getProductDetailsForReview");
        return form;
    }

CORS过滤器:

package com.bullbeardevice.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

/**
 * Servlet Filter implementation class SimpleCORSFilter
 */
@Component
public class SimpleCORSFilter implements Filter {

    /**
     * Default constructor. 
     */
    public SimpleCORSFilter() {
        // TODO Auto-generated constructor stub
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        System.out.println("In CORS Filter");
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

回复:

Remote Address:127.0.0.1:8888
Request URL:http://localhost:8888/getProductDetailsForReview.html
Request Method:POST
Status Code:415 Unsupported Media Type
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:23
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:8888
Origin:http://localhost:8080
Referer:http://localhost:8080/samplekit/jsp/index.jsp
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Form Dataview sourceview URL encoded
{"productMasterId":"1"}:
Response Headersview source
Access-Control-Allow-Headers:application/javascript
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Cache-Control:no-cache
Cache-Control:no-store
Content-Length:83
Content-Type:text/html; charset=iso-8859-1
Date:Wed, 15 Oct 2014 07:09:48 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:no-cache
Server:Development/1.0

Spring xml配置:

    <context:component-scan base-package="com.bull.*" />
    <context:annotation-config />
    <bean id="multipartResolver" class="org.gmr.web.multipart.GMultipartResolver">
        <property name="maxUploadSize" value="1048576" />
    </bean>
    <bean id="viewResolver2"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="ApplicationResources" />
    </bean>
    <mvc:annotation-driven
        content-negotiation-manager="contentNegotiationManager" />
    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean>
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter" />
            </list>
        </property>
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="ApplicationResources" />
    </bean>
    <bean id="contentNegotiationManager"
        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="defaultContentType" value="application/json" />
        <property name="favorPathExtension" value="false" />
    </bean>

</beans>

2 个答案:

答案 0 :(得分:1)

嗯,我想说的有一些信息缺失,但是试着提供帮助。很少有事情可能是错的。从最明显的

开始

您的请求定位于&#34; http://XXXXXXXX.com/getProduct.html&#34;,但您的映射是&#34; /getProductDetailsForReview.html",但我认为这是一个错字。您在评论中提到的问题很可能是由于响应中的内容类型错误。

假设您正在使用CORS过滤器,例如在接受的答案CORS Filter not working as intended中建议。以下将起作用

<script>
    function submitLogin() {
        var obj = {
            "productMasterId": "1"
        };

        $.ajax({
            url: "http://XXXXXXXX.com/getProductDetailsForReview",
            type: 'POST',
            crossDomain: true,
            success: function (response) {
                alert("done");
                alert(response);
            },
            error: function (response) {
                alert(response);
            }
        });

    }
</script>
<a href="javascript:submitLogin()">click </a>

请注意,我已删除了contentType,并且我已从请求中删除了html。原因是如果存在html,则响应的contentType将设置为text / html,与您在请求中设置的不同

@RequestMapping("/getProductDetailsForReview")
    public @ResponseBody PaymentForm getProductDetailsForReview(@RequestBody PaymentForm paymentForm, HttpServletResponse response) {
        log.debug("Start of method getProductDetailsForReview");
        PaymentForm form = userPaymentService.getProductForReview(paymentForm);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        log.debug("End of method getProductDetailsForReview");
        return form;
    }

结合三者,正确的CORS过滤器,略微修改的Ajax调用和修改后的服务器映射,您将使其正常工作, 最好


反映您在更新中的评论。 Spring MVC使用内容协商来推断响应内容类型应该是什么,后缀优先于其他选项。我认为这是你的问题,请看看

http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

这就是说,尝试将 favorPathExtension =&#34; false&#34; 属性添加到Spring MVC配置中,

 <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
...
        <property name="favorPathExtension" value="false" />
...
</bean>

让我知道它是如何锻炼的

答案 1 :(得分:1)

问题只是我在控制器中的方法参数中使用了@RequestBody。现在我用@requestparam替换了它,它工作正常。