2011-06-01 77 views
1

我有下面的代碼片段中,我想返回值從ajax.but我得到以下異常jQuery的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")); 
      }); 
     }); 

編輯

如果爲此有其他的方式,請分享一些鏈接或代碼

回答

1

我想出issue.Below是完整的源代碼(在Web服務無變化)

<%@ 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> 
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> 

JSON的ASP: http://code.google.com/p/aspjson/

+0

現在,我得到這個異常**無效的JSON基元:name ** – 2011-06-01 16:58:18

+0

test with:data:'name ='+ user, – cranberies 2011-06-01 17:03:41

+0

again error again – 2011-06-01 17:10:31

2

試試這個

$.ajax({ 

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

仍然我越來越undefined – 2011-06-01 17:00:36

+0

但你沒有得到'請求格式無效'? – 2011-06-01 17:08:55

+0

我得到這個無效的JSON基元:名稱 – 2011-06-01 17:13:34