views:

49

answers:

2

Using this tutorial: http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx

1) The web service is invoked such as Service1.asmx/HelloToYou

The default web service in asp.net however won't load the page with this rewritten url, instead i can only refer to it as: Service1.asmx?op=HelloToYou

How do I implement the so-called url rewrite here?

2) the default asp.net web service: is it JSON format? it's not clear how and where i specify the format.

On the Jquery side i do something like:

$.ajax({
                 type: "POST",
                 url: "WebService/Service1.asmx/HelloToYou",
                 data: "{'name': '" + $('#name').val() + "'}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function(msg) {
                     AjaxSucceeded(msg);
                 },
                 error: AjaxFailed
             });

so the content-type is JSON.

In asp.net 3.5, do I have to specifically set the format to JSON or is it JSON by default?

Thanks!

UPDATE: in web service code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace DummyWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod()]
        public string HelloToYou(string name)
        {
            return "Hello " + name;
        }

        [WebMethod()]
        public string sayHello()
        {
            return "hello ";
        }
    }

}
A: 

You need to uncomment some lines at code behind file.

Zote
+1  A: 

I used to declare a specific c# class for the json response. If you set the attribute [Serializable] above it, it will be serialized during the response to the client.

Something like:

[Serializable]
public class json_response
{
    public bool response { get; set; }

    public json_response() { }

    public json_response(bool response)
    {
     this.response = response;
    }
}

then, in a method you can:

[WebMethod()]
public json_response method()
{
    /* your stuff */

    return new json_response(/* your result */);
}

by javascript you can handle the json simply:

...
success: function(msg) {
                     /* in the msg.d.response you'll find your c# boolean variable */
                 },

...

For your example, just use a string proprerty in the json_response class.

tanathos
actually, it turns out that asp.net starting 3.5 are json by default. it works!
gnomixa