日期:2014-05-18  浏览次数:20396 次

NET2.0+jQuery Ajax+webservice老是出错,求解
我按照网上提供的标准AJAX+WebService的代码写了个AJAX,但始终不执行WebService里面的内容,麻烦帮忙解答一下。
执行的结果始终弹出“错误”二字,感觉根本就没有执行到WebService
C# code
<script language="javascript" type="text/javascript" src="/JavaScript/jquery.js"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $('#Login').click(function() {
               $.ajax({
                    type: "POST",   //访问WebService使用Post方式请求
                    contentType: "application/json;utf-8", //WebService 会返回Json类型
                    url: "/User/UserLogin.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
                    data: "{}",         //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到       
                    dataType: "json",
                    success: function(result) {     //回调函数,result,返回值
                        try {
                            alert(result.d);
                        }
                        catch (e) {
                            alert(e)
                        }
                    },
                    error: function(x, e) {
                        alert("错误");
                    }
                });
            });
        });
    </script>

C# code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;


namespace Food5u.Web.User
{
    /// <summary>
    /// UserLogin 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class UserLogin : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}




------解决方案--------------------
webservice中的 [System.Web.Script.Services.ScriptService]这句呢
人家不是说的好好的,如果允许客户端调用webservice就把这句注释去掉,你怎么把这句删除了
------解决方案--------------------
要这样才行,把那句的注释符去掉

C# code
/// <summary>
    /// Summary description for WebService1
    /// </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 WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }