3

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.

35
  • Make sure you don't have a <</script> tag at the end. Commented Dec 27, 2012 at 10:55
  • 1
    This could be the combination of POST and jsonp which are not compatible without some hacking - see stackoverflow.com/questions/2699277/post-data-to-jsonp Commented Dec 27, 2012 at 11:03
  • 1
    I've not done any asp.net but stackoverflow.com/questions/2380551/… may help. The problem seems to be that you are using jsonp which adds ?callback=? to the end of the URL. The handler (your Web Service) for the URL must also be coded to return a padded response i.e. a response wrapped in a JavaScript function call. I suspect you just want to be doing a simple ajax GET to that URL that just returns a simple JSON response. Commented Dec 27, 2012 at 11:18
  • 1
    @Liam ERROR 500, if I use url: "http://localhost:12015/myWebService.asmx/GetCategories" and ERROR, unexpected token < If I use url: "http://localhost:12015/myWebService.asmx?op=GetCategories" Commented Dec 27, 2012 at 11:32
  • 1
    Woaah there! So when you run the site containing the javascript what it's url? Does it start localhost:12015? If not you can't do this it breaks a security protocol: en.wikipedia.org/wiki/Same_origin_policy. Which is what @andyb said at the start. If it's a different project in the same solution it will be a different site with a different url. Commented Dec 27, 2012 at 11:50

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Sorry I can't participate in the chat but this seems consistent with the comments in the question above :-)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.