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.

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • Make sure you don't have a `<` tag at the end. – Blender Dec 27 '12 at 10:55
  • @Blender no there is not.. no error with code right? – Mr_Green Dec 27 '12 at 10:56
  • When does this error happen (after your AJAX call or when the script loads)? – Blender Dec 27 '12 at 10:56
  • Can you grab and post your Json off the wire? – Liam Dec 27 '12 at 10:57
  • @Blender I think AJAX, because I can see the 'error' alert message. – Mr_Green Dec 27 '12 at 10:57
  • by post I mean append it to the question btw – Liam Dec 27 '12 at 10:59
  • @Liam this is all how I am calling to json.. there is no further code related to it.. (I think) – Mr_Green Dec 27 '12 at 11:00
  • What I mean is use http://www.fiddler2.com/fiddler2/ or something similar to look at the data sent between your client and server. Then you can actually see the json being generated, what it looks like, etc. – Liam Dec 27 '12 at 11:01
  • are you sure the return data type is json, not xml? – Karthik Dec 27 '12 at 11:02
  • 1
    This could be the combination of `POST` and `jsonp` which are not compatible without some hacking - see http://stackoverflow.com/questions/2699277/post-data-to-jsonp – andyb Dec 27 '12 at 11:03
  • the attribute says `ResponseFormat = ResponseFormat.Json` so it should be – Liam Dec 27 '12 at 11:03
  • Is http://localhost:12015 the same as url as the site running the js? If so @andyb's answer doesn't cause an issue as that is only relevant cross domains. if it's not he could have it. – Liam Dec 27 '12 at 11:05
  • @Liam yes the port is same. I am sure about it – Mr_Green Dec 27 '12 at 11:06
  • @andyb if I use `json` instead of `jsonp` in 'dataType' and 'contentType' then I am getting `error 405(method not loaded)` and `xmlhttprequest cannot load`. – Mr_Green Dec 27 '12 at 11:12
  • HTTP Status Code `405` is [`method not allowed`](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6). Does the web service support `POST` on that URL? – andyb Dec 27 '12 at 11:12
  • @andyb I am not sure about it.. anyway to check it? – Mr_Green Dec 27 '12 at 11:14
  • 1
    I've not done any `asp.net` but http://stackoverflow.com/questions/2380551/jquery-success-function-not-firing-using-jsonp 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. – andyb Dec 27 '12 at 11:18
  • Fire up fiddler and this will all become clear! You need to see the HTTP on the wire. – Liam Dec 27 '12 at 11:22
  • @Liam I dont know how to use it.. anyhow I just run fiddler2 and executed my project and checked the `json` tab of `inspectors`, it is empty. – Mr_Green Dec 27 '12 at 11:26
  • @andyb I changed `type: 'GET'` and added `jsonpCallback: 'mycallback',` now the error is `unexpected token <`. – Mr_Green Dec 27 '12 at 11:27
  • Thats the problem then! Your service isn't returning any Json. Whats the `Raw` response? Is it a http 500 or something? – Liam Dec 27 '12 at 11:29
  • what happens when you change the datatype to xml instead of json? – Karthik Dec 27 '12 at 11:30
  • 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"` – Mr_Green Dec 27 '12 at 11:32
  • @Karthi.L no way to change it to xml or anything else (restricted to me) Need to get it by using json only. – Mr_Green Dec 27 '12 at 11:34
  • Right so the call to your webservice is failing. Whats the Raw request? Your request is badly formatted for some reason and the webservice is throwing an exception so the response doesn't contain Json and your call fails. – Liam Dec 27 '12 at 11:35
  • OK http://localhost:12015/myWebService.asmx?op=GetCategories is only what you use when calling through a browser to test so your call should be http://localhost:12015/myWebService.asmx/GetCategories. But this returns a 500 error? Can you debug the service? – Liam Dec 27 '12 at 11:39
  • @Liam No, I cant, the web service is in different project of same solution. – Mr_Green Dec 27 '12 at 11:46
  • 1
    Woaah there! So when you run the site containing the javascript what it's url? Does it start http://localhost:12015/? If not you can't do this it breaks a security protocol: http://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. – Liam Dec 27 '12 at 11:50
  • It has different URL: 11761.. How to solve this then? please help. – Mr_Green Dec 27 '12 at 11:54
  • @Liam I tried to change the port from properties --> web but it automatically changed to other port number `58300`. any other way to change it? – Mr_Green Dec 27 '12 at 11:59
  • you can't run two sites on the same port. they need to be unique. You need to move the webservice into the web site so they are on one project. – Liam Dec 27 '12 at 12:02
  • @Liam I am not using the other project(web service) in my page. I am giving web reference of that web service to my page. – Mr_Green Dec 27 '12 at 12:04
  • you can't post to a webservice on another url. It's impossible. It breaks a fundamental security policy. If you need to post to the webservice it needs to be in your web site. have you tried using a get? – Liam Dec 27 '12 at 12:06
  • @Liam If you dont mind can we discuss this topic [**here**](http://chat.stackoverflow.com/rooms/16765/stick-organizer) (others are welcome too) I tried using get. ERROR -- `unexpected token` – Mr_Green Dec 27 '12 at 12:08
  • Related [Link](http://stackoverflow.com/q/11563064/1577396) – Mr_Green Dec 28 '12 at 05:33

2 Answers2

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!

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
  • +1 Sorry I can't participate in the chat but this seems consistent with the comments in the question above :-) – andyb Dec 27 '12 at 12:40
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.

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418