使用Ajax从jQuery调用WCF服务,但参数为';nt已通过。(ASP.NET)

使用Ajax从jQuery调用WCF服务,但参数为';nt已通过。(ASP.NET),jquery,asp.net,ajax,wcf,Jquery,Asp.net,Ajax,Wcf,我正在开发Microsoft Visual Studio 2013社区版(WebForms,Visual Basic) 在jQuery脚本中,使用Ajax调用WCF服务 当我按下“LogInMe”按钮时,在“Function fnVerifyID(ByVal ID作为字符串,ByVal PASS作为字符串)”中,“ID”和“PASS”是“Nothing”。(我在firefox调试器中看到了它。) 但是,在使用“data:{ID:“testID”,PASS:“testPASS”}(注释中)的情况下

我正在开发Microsoft Visual Studio 2013社区版(WebForms,Visual Basic)

在jQuery脚本中,使用Ajax调用WCF服务

当我按下“LogInMe”按钮时,在“Function fnVerifyID(ByVal ID作为字符串,ByVal PASS作为字符串)”中,“ID”和“PASS”是“Nothing”。(我在firefox调试器中看到了它。)

但是,在使用“data:{ID:“testID”,PASS:“testPASS”}(注释中)的情况下,由于某种原因,参数是按预期传递的,而不是“para”

【Test.aspx】

<%@ Page Title="" Language="vb" AutoEventWireup="false"     MasterPageFile="~/Site.Mobile.Master" CodeBehind="Test.aspx.vb" Inherits="Shop.Daishin" %>
    <%--<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>--%>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">

<head>
    <!DOCTYPE html> 
<title>Test Title</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css"/>
<link rel="stylesheet" href="css/custom.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <%--<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>--%>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script src="js/jCarousel.min.js" type="application/x-javascript" charset="utf-8"></script>
<script src="js/custom.js"></script>


    <script type="text/javascript">

    $(function () {

        $('#LogInMe').click(function () {

            var Para = '{ID: ' +  '"'+ $('#txtUserName').val() +'"'+ ', PASS: ' + '"'+ $('#txtPassword').val() + '"}'

            $.ajax({
                url: '/TestWCF.svc/fnVerifyID', 

                dataType: 'json', 
                contentType: "application/json; charset=utf-8",

                //data: { ID: "testID", PASS: "testPASS" },    // This is OK.
                data: Para,

                success: function (response) {
                        var obj = JSON.parse(response.d);            
                        alert(obj.Name)
                }, 

                error: function (xhr, status, err) {
                    alert('Exeption:');

                } 
            });

        });
    });

</script>

</head>
<body>

  <div data-role="page" data-add-back-btn="true" id="p-listview-thumbnail" >

        <div data-role="header" data-position="fixed" data-theme="d">
           <h1></h1>
            <a href="#Family" data-icon="gear" class="ui-btn-right" data-iconpos="notext" data-theme="d" >Options</a>   
            <input type="text" id="txtUserName" placeholder="Username" />
            <input type="password" name="passwordinput" id="txtPassword"  placeholder="Password" value=""  />
            <a href="javascript:Authenticate();" id="LogInMe" data-role="button" data-icon="check" data-iconpos="right">Log Me In!</a>
             <label for="basic_name" id="lblMessage"></label>            
        </div><!-- /content -->

</body>
</html>

试试这个:

var jData = {};
jData.ID = $('#txtUserName').val();
jData.PASS = $('#txtPassword').val();
$.ajax({
    crossDomain: true,
    cache: false,
    url: '/TestWCF.svc/fnVerifyID',
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: JSON.stringify(jData),
    success: SuccessRslt,
    error: function (xhr, reason, ex) {
        var err = $.parseJSON(xhr.responseText);
        if (err != null && xhr.status.toString() != "0") {
            showerror('Error Code : ' + xhr.status + "\nError Message :" + xhr.statusText);
        }
    }
});

为什么对
Para
使用字符串而不是对象?甚至它也不应该成为一个问题。或者,您应该为输入设置name属性,并使用:
data:$(this).closest('div').find(':input').serialize()或更好使用表单并序列化非常感谢您的回答。我似乎不理解基本内容…非常感谢您提供的示例代码。此代码工作成功。
var jData = {};
jData.ID = $('#txtUserName').val();
jData.PASS = $('#txtPassword').val();
$.ajax({
    crossDomain: true,
    cache: false,
    url: '/TestWCF.svc/fnVerifyID',
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: JSON.stringify(jData),
    success: SuccessRslt,
    error: function (xhr, reason, ex) {
        var err = $.parseJSON(xhr.responseText);
        if (err != null && xhr.status.toString() != "0") {
            showerror('Error Code : ' + xhr.status + "\nError Message :" + xhr.statusText);
        }
    }
});