繁体   English   中英

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

[英]Jquery ajax return exception(Request format is invalid)

我有以下代码片段,我想从 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"));
            });
        });

编辑

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

尝试这个

   $.ajax({

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

尝试这个:

    [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 用于 ASP: http://code.google.com/p/aspjson/

我发现了问题。下面是完整的源代码(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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM