Jquery ajax返回异常(请求格式无效)

时间:2011-06-01 16:40:40

标签: c# jquery asp.net ajax

我有以下代码片段,其中我想从ajax返回值。但我得到以下异常

请求格式无效

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string HelloWorld(string name) {
    return "Hello World"+name;
}


 $(document).ready(function () {
            function checkUser2(user) {
                var result;
                $.ajax({

                    type: "POST",
                    async: false,
                    url: "WebService.asmx/HelloWorld",
                    dataType: "json",
                    contentType:"application/json",
                    data: { name: user},
                    success: function (data) {
                        result = data;
                    }
                });
                return result;
            }
            $("#check").click(function () {
                alert(checkUser2("test"));
            });
        });

修改

如果您有其他方法,请分享一些链接或代码

3 个答案:

答案 0 :(得分:2)

试试这个

   $.ajax({

               ...
                data: "{ 'name': 'user'}",
                ...
            });

答案 1 :(得分:1)

试试这个:

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public string HelloWorld(string name) {
        return "{'message':'Hello World'}";
    }


    -----
    <script type="text/javascript">
$(document).ready(function () {
            function checkUser2(user) {
                var result;
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "WebService.asmx/HelloWorld",
                    dataType: "json",
                    data:{name:user},
                    success: function (data) {
                        result = data.message;
                    }
                });
                return result;
            }
            $("#check").click(function () {
                alert(checkUser2("test"));
            });
        });
</script>
ASP的json: http://code.google.com/p/aspjson/

答案 2 :(得分:1)

我想出了问题.Below是完整的源代码(webservice没有变化)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <title></title>
    <script type="text/javascript">
        $(document).ready(function () {
            function checkUser2(name, callback) {
                $.ajax({

                    type: "POST",
                    async: tr,
                    // url: "Handler.ashx",
                    url: "WebService.asmx/HelloWorld",
                    data: "{name:'" + name + "'}",
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        callback(data.d);
                    }
                });
            }
            $("#check").click(function () {
                checkUser2("test", function (d) {
                    var a = d;
                    alert(a);
                });
            });
        });


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" name="check" value="check " id="check" />
    </div>
    </form>
</body>
</html>