ASP.net和Jquery问题

ASP.net和Jquery问题,asp.net,jquery,json,.net-3.5,Asp.net,Jquery,Json,.net 3.5,使用本教程: 1) web服务被调用,例如 Service1.asmx/hellotyou 但是,asp.net中的默认web服务不会加载此重写url的页面,我只能将其称为: Service1.asmx?op=HelloToYou 我如何在这里实现所谓的url重写 2) 默认的asp.net web服务:是JSON格式吗?不清楚我如何以及在何处指定格式 在Jquery方面,我做了如下操作: $.ajax({ type: "POST", url: "WebService/Service1

使用本教程:

1) web服务被调用,例如 Service1.asmx/hellotyou

但是,asp.net中的默认web服务不会加载此重写url的页面,我只能将其称为: Service1.asmx?op=HelloToYou

我如何在这里实现所谓的url重写

2) 默认的asp.net web服务:是JSON格式吗?不清楚我如何以及在何处指定格式

在Jquery方面,我做了如下操作:

$.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
});
所以内容类型是JSON

在asp.net 3.5中,我是否必须专门将格式设置为JSON,还是默认设置为JSON

谢谢

更新:在web服务代码背后

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 ";
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
名称空间DummyWebService
{
/// 
///服务1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
[WebMethod()]
公共字符串HelloToYou(字符串名称)
{
返回“Hello”+name;
}
[WebMethod()]
公共字符串sayHello()
{
回复“你好”;
}
}
}

您需要取消注释代码隐藏文件中的一些行。

我曾经为json响应声明一个特定的c#类。 如果将属性[Serializable]设置在其上方,则在响应客户机期间将对其进行序列化

比如:

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

    public json_response() { }

    public json_response(bool response)
    {
        this.response = response;
    }
}
然后,在一种方法中,您可以:

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

    return new json_response(/* your result */);
}
通过javascript,您可以简单地处理json:

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

...

对于您的示例,只需在json_响应类中使用字符串属性。

也发布HelloToYou web方法的内容。否则我们将不知道该方法返回的数据类型。事实上,从3.5开始的asp.net默认为json。它起作用了!