Uncaught SyntaxError: Unexpected token < -- in jQuery ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14053484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:13:46  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token < -- in jQuery ajax

jqueryasp.netajaxweb-services

提问by Mr_Green

I am new to web service. In my project, I connected Web Service(everything is ready-made) now when I tried to run I got the below error.

我是网络服务的新手。在我的项目中,当我尝试运行时,我现在连接了 Web 服务(一切都是现成的),但出现以下错误。

ERROR -->

错误-->

Uncaught SyntaxError: Unexpected token <

Uncaught SyntaxError: Unexpected token <

The Web service and my page are in same solution but different projects.

Web 服务和我的页面在相同的解决方案中,但在不同的项目中。

The related code is as follows:

相关代码如下:

jQuery(URL: 11761)

jQuery(网址:11761)

 function GetAllCategories() {
   $.ajax({
      url: "http://localhost:12015/myWebService.asmx?op=GetCategories",
      type: "POST",
      dataType: "jsonp",
      data: "{}",
      contentType: "application/jsonp; charset=utf-8",
      success: function (data) {
          var categories = data.d;
          $.each(categories, function (index, category) {
              alert(category.CategoryId);
          });
      },
      error: function (e) {
          alert(e.message);
      }
   });
}

Web Service(URL: 12015)

网络服务(网址:12015)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Categories>  GetCategories()
{
    //Code
}

Before asking here I have gone through this link(cant understand it)

在问这里之前,我已经浏览了这个链接(看不懂)

EDIT:

编辑:

Got alternative answer from this post.

从这篇文章中得到了替代答案。

回答by Liam

Figured it out your breaking the Same origin policyas your site and web service are running in two different projects.

发现您违反了同源策略,因为您的站点和 Web 服务在两个不同的项目中运行。

Move the Webservice and website into the same projectand it should work.

Webservice 和网站移到同一个项目中,它应该可以工作。

Also your javascript is wrong it should be

你的javascript也错了,应该是

function GetAllCategories() {
   $.ajax({
      url: "http://localhost:12015/myWebService.asmx/GetCategories",
      type: "POST",
      dataType: "jsonp",
      data: "{}",
      contentType: "application/jsonp; charset=utf-8",
      success: function (data) {
          var categories = data.d;
          $.each(categories, function (index, category) {
              alert(category.CategoryId);
          });
      },
      error: function (e) {
          alert(e.message);
      }
   });
}

the op bit is only there for testing in a web browser. Remove that and you should be good.

op 位仅用于在 Web 浏览器中进行测试。删除它,你应该很好。

PS @andyb in fairness suggested this answer a while ago but it wasn't clear that this was the problem! UPDATE

PS @andyb 公平地说,不久前提出了这个答案,但不清楚这是问题所在! 更新

Been doing a bit off jiggery pokery around this today and I've clarified a few points that I thought I'd share. you have to POSTto an .asmxservice and you cannot do this cross domain. You could fire a GETacross domains but not a POSTso this is the route issue I believe.

今天一直在做一些关于这个的jiggery pokerry,我已经澄清了一些我认为我会分享的观点。您必须POST使用.asmx服务,而不能跨域执行此操作。您可以GET跨域触发 a但不能触发 aPOST所以这是我认为的路由问题。

You can enable GET, see How to call an ASMX web service via GET?. But this seems like a bad idea as it would expose your webservice to all and sundry!

您可以启用GET,请参阅如何通过 GET 调用 ASMX Web 服务?. 但这似乎是一个坏主意,因为它会将您的网络服务暴露给所有人!

回答by LCJ

Not a direct answer to your question - but following is what I had to do to resolve similar issue

不是对您问题的直接回答 - 但以下是我为解决类似问题而必须做的

How to force datatype as JSON in ASP.Net 2.0

如何在 ASP.Net 2.0 中强制数据类型为 JSON

The problem was that ASP.Net Ajaxwas not installed and configured.

问题是ASP.Net Ajax没有安装和配置。