Ajax POST多个参数到WCF

时间:2015-10-27 13:28:43

标签: jquery ajax wcf

我想知道如何以及是否可以使用JQuery和AJAX向WCF服务发送多个参数。

让我解释一下......

我需要向WCF函数发送一个名称,一个代码和一个电子邮件地址,然后对其进行处理。我的POST AJAX函数如下所示:

    function AddCustomer() {
    var CustomerData = {
        "CustomerName": CustomerName,
        "CustomerCode": CustomerCode,
        "CustomerEmail": CustomerEmail
    };
    $.ajax({
        type: "POST",
        url: "URL/SERVICE/AddCustomer/" + CustomerCode,
        data: JSON.stringify(CustomerData),
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        processData: true,
        success: function (data, status, jqXHR) {
            alert("success..." + data);
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });
};

通过运行上述功能,我可以将代码作为参数传递(或者看起来如此)。当我尝试添加更多参数时,我得到了ParserErrors。 我想将CustomerName,CustomerCode,CustomerEmail发送到服务器上的WCF SUB。 WCF SUB的实现看起来像;

 <OperationContract()> _
    <WebInvoke(Method:="*", BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json,
               ResponseFormat:=WebMessageFormat.Json, UriTemplate:="AddCustomer/{Code}")> _
    Sub AddCustomer(ByVal JSONCustomer As Customers, ByVal Code As String)

SUB看起来像:

       Public Sub AddCustomer(ByVal JSONCustomer As Customers, ByVal Code As String) Implements ISERVICE.AddCustomer

        Try

            Dim Cust As New Customers

                        System.Diagnostics.EventLog.WriteEntry("SERVICE", "YAY!", EventLogEntryType.Information)


        Catch ex As Exception

            ErrorMessage = ex.Message
            System.Diagnostics.EventLog.WriteEntry("SERVICE", ErrorMessage, EventLogEntryType.Error)
        End Try

    End Sub

任何人都可以帮我将所有数据正确传递到我的WCF服务吗?

1 个答案:

答案 0 :(得分:0)

我会尝试这样的事情:

var CustomerData = {
    "JSONCustomer" : {
          "Name": CustomerName,
          "Email": CustomerEmail
    },       
    "Code": CustomerCode     
};

假设NameEmailCustomers class

的属性

此外,您的UriTemplate应该是UriTemplate:="AddCustomer/{Customer}/{Code}"