简体   繁体   English

跨域Ajax(JSONP)回调问题

[英]Cross Domain Ajax (JSONP) Callback Issue

I have a code where i need to make cross domain requests to get the text/html as response. 我有一个代码,在这里我需要进行跨域请求以获取text / html作为响应。 I've been using the JSONP method and was getting the results (json) before on some other program. 我一直在使用JSONP方法,并在其他程序上获取结果(json)。 When i run the program, console reports an error which is 当我运行程序时,控制台报告一个错误,该错误是

 Uncaught SyntaxError: Unexpected token < 
 ?callback=jQuery17207555819984991103_1414157672145&_=1414157675836"

As you can see it has added an extra parameter value after the callback parameter which is causing an issue. 如您所见,它在导致问题的回调参数之后添加了一个额外的参数值。 When i see the response in Network tab, its pretty good and exactly what i wanted. 当我在“网络”选项卡中看到响应时,它相当不错,而且正是我想要的。 I tested it on 4 cross domain links and that extra paramter is coming to all of them. 我在4个跨域链接上进行了测试,所有这些参数都将受到额外的影响。 Following is my code: 以下是我的代码:

$.ajax({
                type: 'get',
                url: link,
                dataType: 'jsonp',
                success: function(dataWeGotViaJsonp){
                    alert(dataWeGotViaJsonp)
                    var len = dataWeGotViaJsonp.length;
                }
            });

The links I have passed to it: 我传递给它的链接:

 http://stackoverflow.com/questions/20944962/data-grid-view-update-edit-and-delete-in-vb-net-windows-form-that-using-multipl
 http://www.espncricinfo.com/pakistan-v-australia-2014/engine/match/727927.html
 http://pucit.edu.pk/

Response is good but due to that error, success callback isn't being called. 响应很好,但是由于该错误,未调用成功回调。 Any solutions? 有什么办法吗?

"Uncaught SyntaxError: Unexpected token <" is an indication that the returned data is very likely HTML mark-up and not proper JSONP. “未捕获的SyntaxError:意外的令牌<”表示返回的数据很有可能是HTML标记,而不是正确的JSONP。 In order to return HTML from a JSONP web service, you need something on the server that is wrapping the HTML in proper procedure call syntax. 为了从JSONP Web服务返回HTML,您需要在服务器上以正确的过程调用语法包装HTML的内容。 Eg, 例如,

jQuery17207555819984991103_1414157672145 ("<div>... your HTML here ...</div>")

Since the HTML will likely have quote characters in it somewhere, you will need to escape, URL encode, UUEncode, or otherwise convert the HTML text to be an appropriate Javascript string, and then convert it back in the client. 由于HTML可能在某处带有引号,因此您需要转义,URL编码,UUEncode或将HTML文本转换为适当的Javascript字符串,然后再将其转换回客户端。

As for the " _ =1414157675836", this is being added to ensure the response is not cached. 至于“ _ = 1414157675836”,正在添加它以确保不缓存响应。 Unless your server's web service is rejecting the request because the " _ " parameter is not recognized, this is a red herring. 除非您的服务器的Web服务因为无法识别“ _ ”参数而拒绝该请求,否则这是一个红色鲱鱼。 The problem is the bad JSONP syntax coming from the host. 问题是来自主机的JSONP语法错误。

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

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