如何解决JSONP未捕获的语法错误 [英] How to resolve uncaught syntax error in jsonp

查看:234
本文介绍了如何解决JSONP未捕获的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问使用jQuery的ajax跨域API,但我无法实现这一目标。荫收到错误为未捕获的SyntaxError:意外的标记<在控制台中,如何解决这个问题。

code:

  $。阿贾克斯({
网址:HTTP:// ..............
键入:GET,
数据类型:JSONP,
跨域:真正的,
的contentType:应用程序/ JavaScript的,
成功:功能(数据){
    警报(数据);
},
错误:函数(errormessage的,Errtext){
    警报(Errtext);
}
});
 

错误:

 未捕获的SyntaxError:意外的标记<
 

解决方案

您无法通过JSONP到返回XML服务器跨域AJAX调用。如果你希望能够做一个跨域AJAX调用有两种方式:

  • 使用 JSONP - >您服务器需要支持它。
  • 使用 CORS - >你的服务器客户端浏览器需要科技支撑的。

如果您的服务器支持CORS您的请求可能是这样的:

  $。阿贾克斯({
    网址:HTTP:// ..............
    键入:GET,
    跨域:真正的,
    成功:功能(数据){
        警报(数据);
    },
    错误:函数(errormessage的,Errtext){
        警报(Errtext);
    }
});
 

I am accessing a cross domain api using jquery ajax but I can't achieve it . Iam getting error as "uncaught syntaxerror : unexpected token <" in the console, How to fix this.

Code:

$.ajax({ 
url: "http://..............", 
type:"GET", 
dataType: 'jsonp', 
crossDomain: true, 
contentType:"application/javascript", 
success: function (data) { 
    alert(data);
}, 
error: function (errorMEssage, Errtext) { 
    alert(Errtext);
} 
});

Error:

uncaught syntaxerror : unexpected token <

解决方案

You cannot make cross domain AJAX calls using JSONP to a server that returns XML. If you want to be able to make a cross domain AJAX call you have 2 possibilities:

  • use JSONP -> your server needs to support it.
  • use CORS -> your server AND client browser need t support it.

If your server supports CORS your request may look like this:

$.ajax({ 
    url: "http://..............", 
    type:"GET", 
    crossDomain: true, 
    success: function (data) { 
        alert(data);
    }, 
    error: function (errorMEssage, Errtext) { 
        alert(Errtext);
    } 
});

这篇关于如何解决JSONP未捕获的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆