简体   繁体   English

内容类型未在来自两个不同端口的JSONP请求上设置

[英]Content Type Not being set on JSONP Request From Two Different Ports

This situation is killing me. 这种情况使我丧命。 A JSONP Jquery Ajax request from http://example.com:80 to http://example1.com:8080 never sets the Content Type and Spring MVC complains on the server when trying to serialize my javabean in the controller parameter. http://example.com:80http://example1.com:8080的 JSONP Jquery Ajax请求从不设置内容类型,并且在尝试在controller参数中序列化我的javabean时,Spring MVC在服务器上抱怨。 If I test the JSONP request on either website calling the same domain, it sets the content type. 如果我在两个调用相同域的网站上测试JSONP请求,它将设置内容类型。 Is this a port issue? 这是港口问题吗? I am testing a Wordpress site to Spring MVC API on my local. 我正在本地测试Springpress MVC API的Wordpress网站。

handlerMapperInvoker.java - This is where the error occurs in the Spring Framework handlerMapperInvoker.java-这是Spring框架中发生错误的地方

MediaType contentType = inputMessage.getHeaders().getContentType();
        if (contentType == null) {
            StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
            String paramName = methodParam.getParameterName();
            if (paramName != null) {
                builder.append(' ');
                builder.append(paramName);
            }
            throw new HttpMediaTypeNotSupportedException(
                    "Cannot extract parameter (" + builder.toString() + "): no Content-Type found");
    }

apicontroller.java - API Side apicontroller.java-API端

@RequestMapping(value="/badge/get")
public @ResponseBody IHttpResponse getBadge(@RequestBody GetBadgeRequest request) {

    apiService.getBadge(request);

    return request.getResponse();
}

api.js - Wordpress api.js-Wordpress

getBadge : function(id) {

        var model = 
        {
            id : id
        };

        this.call({ url: 'badge/get.json', type: 'GET', data: model, callback: function(data)
        {
            alert(data);
        }});
    },

call : function(options) {

        var def = {
            url : '',
            type : "POST",
            dataType : 'jsonp',
            data : undefined,
            contentType : "application/json",
            callback : function() {},
            errorCallback : function() {}
        };

        $.extend(def, options);

        var sessionToken = api.getSession();

        if(sessionToken && sessionToken != "undefined") {
            if(!def.data) {
                def.data = {};
            }

            def.data.sessionToken = sessionToken;
        }

        var url = config.baseUrl + def.url;

        // Abort Request If Another One Is Made
        if (config.request != null)
            config.request.abort();

        if (def.showLoader) {
            application.loader(true);
        }

        config.request = $
                .ajax({
                    url : url,
                    type : def.type,
                    data : { request: $.toJSON(def.data) },
                    dataType : def.dataType,
                    contentType : def.contentType,
                    success : function(data) {
                        if (def.showLoader) {

JSONP requests outside the current domain are always made as script requests to get around the same origin policy. 当前域之外的JSONP请求始终作为脚本请求发出,以绕过相同的原始策略。 The parameters will be encoded into the url (ie, no POST, only a GET for a script). 参数将被编码到url中(即,没有POST,只有脚本的GET)。 Since there's no way to specify a content type on a script request, it doesn't appear. 由于无法在脚本请求中指定内容类型,因此不会出现。 When done locally, it keeps the content type since it's actually making a real AJAX request. 在本地完成时,由于它实际上是在发出真正的AJAX请求,因此它会保留内容类型。

Essentially what an AJAX request for JSONP to a different domain translates to is: 从本质上讲,将JSONP发送到不同域的AJAX请求是:

<script type="text/javascript" src="http://example1.com:8080/badge/get.json?request=...&callback=somefunction">
</script>

And it expects to receive in return a script like: 它期望收到如下脚本:

somefunction( { ... your json here ... } );

Note when using jQuery somefunction is actually an automatically generated name. 请注意,使用jQuery时, somefunction实际上是一个自动生成的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM