简体   繁体   English

解析jsonp时出现“未捕获的SyntaxError:意外令牌”

[英]“Uncaught SyntaxError: Unexpected token” when parse jsonp

I have created a json webservice in ASP.NET. 我已经在ASP.NET中创建了一个json网络服务。

URL http://mydomain:21130/JSONdata.aspx?Option=GetListRootMenus

Data return: 数据返回:

{"NumberOfMenu":"2", "Menus":[{"MenuKey":"menu_home", "MenuLevel":"1" },{"MenuKey":"menu_info", "MenuLevel":"1" }]}

After that, i parsed via JSONP 之后,我通过JSONP进行了解析

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p> this is a param </p>
 <script>
      $(document).ready(function(){
      console.log("zc");
            $.ajax({
                  dataType: "jsonp",
                  type: 'GET',
                  url: "http://mydomain:21130/JSONdata.aspx?Option=GetListRootMenus&callback=?",
                  success: function(data) {
                    console.log("123");
                  },
                  error: function(){
                    console.log("456");
                  }
            });
      });
</script>
</body>
</html>

but wrong. 但是错了。 i don't know why :( 我不知道为什么:( 在此处输入图片说明

As I explained in my comment, your service returns JSON, while jQuery expects JSONP. 正如我在评论中所解释的那样,您的服务返回JSON,而jQuery需要JSONP。 This is an issue because the response will be evaluated as JavaScript, but you are not sending back valid JavaScript. 这是一个问题,因为响应将被评估为JavaScript,但是您没有发送回有效的JavaScript。

Since you have control over the server, you have three possible solutions: 由于您可以控制服务器,因此有三种可能的解决方案:

1. Tell jQuery to expect JSON 1.告诉jQuery接受JSON

If there are no cross-domain issues because the site itself is also served from that domain, simple let jQuery make a real Ajax request, by changing dataType to json and removing callback=? 如果没有跨域问题,因为该站点本身也从该域提供服务,则简单地让jQuery通过将dataType更改为json并删除callback=?来发出真正的Ajax请求callback=? from the URL. 从URL。

2. Enable CORS (and tell jQuery to expect JSON) 2.启用CORS(并告诉jQuery使用JSON)

If there are cross-domain issues, you can still do what I said in the first solution, but in addition, enable CORS on your server. 如果存在跨域问题,您仍然可以按照我在第一个解决方案中所说的做,但是此外,请在服务器上启用CORS。 In short, this allows other sites to make Ajax requests to your server. 简而言之,这允许其他站点向您的服务器发出Ajax请求。 Have a look at http://enable-cors.org/ to learn how to enable it for your server. 请访问http://enable-cors.org/了解如何为您的服务器启用它。

3. Return JSONP 3.返回JSONP

JSONP is all about including JavaScript dynamically. JSONP就是要动态包含JavaScript。 This works across domains because <script> elements don't have the same-origin restriction that Ajax has. 这可以跨域使用,因为<script>元素没有Ajax的同源限制。 However, it is still something that the server has to support because it has to return valid JSON. 但是,服务器仍然必须支持它,因为它必须返回有效的JSON。 If your JSON response was 如果您的JSON响应是

{"foo": 42}

then the JSONP response would be 那么JSONP响应将是

func({"foo": 42});

The name of the function ( func ) in this example, is taken from some GET parameter that you could choose arbitrarily, but most common is callback . 在此示例中,函数的名称( func )取自可以任意选择的一些GET参数,但最常见的是callback jQuery will actually choose a random function name so it will send something like jQuery实际上会选择一个随机函数名称,因此它将发送类似

callback=jQuery123135343456456

your service has to take that value and use it as function name for JSONP, ie 您的服务必须采用该值并将其用作JSONP的函数名称,即

jQuery123135343456456({"foo": 42});

See also 也可以看看

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

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