Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net - Uncaught SyntaxError: Unexpected token < -- in jQuery ajax

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...