0

I wrote the following webservice asmx file in my website:

[WebService(Namespace = "http://eumcore.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class jsonTest : System.Web.Services.WebService {

    public jsonTest () {

    }

    [WebMethod(Description = "Gets the names matching part of a title.")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void getName() {
        List<nameEntry> nameList = new List<nameEntry>();
        nameList.Add(new nameEntry() {id="1", name="John"});
        nameList.Add(new nameEntry() { id = "3", name = "Alex" });
        this.Context.Response.ContentType = "application/json; charset=utf-8";

        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(nameList);
        this.Context.Response.Write(strJSON);
    }    
}

As a start I wanted it to return the same array each time, the result of the webservice when i call it directly is:

[{"id":"1","name":"John"},{"id":"3","name":"Alex"}]

Which is the correct reply, when i use it as a local input the result is fine but when i call the webservice in the input method of tokeninput (I assigned an error message to the function) i get the following error: "200 parsererror undefined"

Could anyone help me figure it out?

Thanks

Doron

EDIT: after playing around with jquery code a bit i managed to receive the data but i get the following error:

200

parsererror

[{"id":"1","name":"aaA"},{"id":"3","name":"aaA"}]{"d":null}

What I don't understand is what is d and why is it null?

6
  • What does when i call the webservice in the input method of tokeninput mean? Commented Aug 1, 2011 at 15:01
  • From the html running the jquery: <script type="text/javascript"> $(document).ready(function() { $("#demo-input").tokenInput("localhost:61965/names/jsonTest.asmx/getName"); }); </script> Commented Aug 1, 2011 at 15:04
  • Is the web page from which you are trying to invoke this webservice also hosted on localhost:61965 or is it in a separate application? Commented Aug 1, 2011 at 15:05
  • It's not part of the application, the file itself is on the machine though. it reaches the destination because i had breakpoints in my code and it stopped Commented Aug 1, 2011 at 15:07
  • It looks like you are trying to perform cross domain AJAX requests which are not allowed. The same origin policy dictates that you must send the AJAX request only to the same domain. The best way to ensure this is to never use absolute urls as you do but only relative urls, like this: $("#demo-input").tokenInput("/names/jsonTest.asmx/getName"); Commented Aug 1, 2011 at 15:07

2 Answers 2

1

You shouldn't be manually serializing that JSON and writing it out. ASP.NET will do that for you automatically if you let it:

[WebMethod(Description = "Gets the names matching part of a title.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<nameEntry> getNames() {
    List<nameEntry> nameList = new List<nameEntry>();

    nameList.Add(new nameEntry() {id="1", name="John"});
    nameList.Add(new nameEntry() { id = "3", name = "Alex" });

    return nameList;
}

The catch is that you need to call the service a particular way with jQuery in order to get a JSON response instead of XML, specifically with a POST request of content-type application/json.

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

2 Comments

For some reason this is outputting the data in xml format eventhough it says json in response format
You need to make sure that you're making the request with a POST that has its content-type set to application/json. If this plugin won't allow you to specify that directly, you can always use $.ajaxSetup() to set those options globally before the plugin runs.
0

Doesn't asp.net wrap the return data in an object for security? For example, you're not getting back the array you thought you were, but instead an object {} that contains your array under the ["d"] property.

I think the plugin requires an array of objects, so you might not be able to pass the url to the tokeninput initializer. Instead, in the callback of your JSON request get the data out and into the right form (an array of objects) and then initialize the tokeninput plugin with that array.

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.