开发者

Jquery ajax return exception(Request format is invalid)

I have following code snippet in which I want to return value from ajax.but I am getting following exception

Request format is invalid

[WebMethod]
[ScriptMethod(ResponseFor开发者_JAVA技巧mat=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"));
            });
        });

EDIT

If you have other way for this please share some link or code


try this

   $.ajax({

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


try this :

    [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>

json for ASP : http://code.google.com/p/aspjson/


I figured out the issue.Below is the complete source code(no changes in 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜