Uncaught SyntaxError: Unexpected token <

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.

ERROR -->

Uncaught SyntaxError: Unexpected token <

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

The related code is as follows:

jQuery (URL: 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)

[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 .


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

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

Also your javascript is wrong it should be

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.

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

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 POST to an .asmx service and you cannot do this cross domain. You could fire a GET across domains but not a POST so this is the route issue I believe.

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!


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

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

链接地址: http://www.djcxy.com/p/45954.html

上一篇: JavaScript函数,将HTML作为参数传递

下一篇: 未捕获的SyntaxError:意外的标记<