Skip to main content
 首页 » 编程设计

c#之jquery ajax返回异常(请求格式无效)

2024年02月23日45sky-heaven

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