none
Ajax POST Multiple Parameters to WCF RRS feed

  • Question

  • I would like to know how and if it is possible to send more than one parameter to a WCF service using JQuery and AJAX.

    Let me explain...

    I need to send A name, a code and an email address to a WCF function and there process it. My POST AJAX function looks like this :
        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);
            }
        });
    };

    By running the above function, I am able to pass ( or so it seems ) the Code as a parameter. When i try to add more parameters, I get ParserErrors. I want to send the CustomerName, CustomerCode, CustomerEmail to a WCF SUB on the server. Or is there a way just to send the entire Object, and use it in WCF? The WCF SUB's Implementation looks like ;

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

    The SUB looks like :

           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
    Can anyone help me getting all the data passed correctly to my WCF Service?
    Wednesday, October 28, 2015 6:38 PM

Answers

  • Hi HanneSThEGreaT,

    According to this case, As far as I know, maybe you can change the datatype as JSON not JSONP.

    As far as I know when we hosted WCF Service on different domain. we must use JSONP. JSONP,

    based on JSON, that allows pages to request data from a server in a different domain, normally

    disallowed.

    Also maybe you can set the following code to your web.config file. Like below:

    <webScriptEndpoint>
          <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
    </webScriptEndpoint>

    This node should be add as a child node to standardEndpoints

    Best Regards,

    Grady

    Tuesday, November 3, 2015 6:24 AM
    Moderator

All replies

  • Hi,

    You have wrong jscript syntax to create object, correct version:

        var CustomerData = {
            CustomerName: "CustomerName",
            CustomerCode: "CustomerCode",
            CustomerEmail: "CustomerEmail"
        };

    Also make sure that VB object Customers has the same structure.

    Wednesday, October 28, 2015 11:40 PM
  • Hi. Thanks for your reply.

    It seems to be communication with the service now. I still have two issues :

    I get a parse error when sending the POST to the service. But as I said, demo code in my service gets executed - so the communication is there. This is my JQuery code :

        function AddCustomer() {
            var CustomerData = {
                "CustomerName": CustomerName,
                "CustomerCode": CustomerCode,
                "CustomerEmail": CustomerEmail
            };
            $.ajax({
                type: "POST",
                url: "http://agilesoft.dyndns.org/UmbrellaMobileService/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, status, error) {
                console.log(JSON.stringify(xhr));
                console.log(JSON.stringify(status));
                console.log(JSON.stringify(error));
                }
            });
        };

    This is my service implementation now:

            <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)
          Public Sub AddCustomer(ByVal JSONCustomer As Customers, ByVal Code As String) Implements IService.AddCustomer
    
                Try
                
                    Dim Cust As New Customers
    
                    System.Diagnostics.EventLog.WriteEntry("USERVICE", "YAY!", EventLogEntryType.Error)
    
    
                Catch ex As Exception
                   
                    System.Diagnostics.EventLog.WriteEntry("SERVICE", ErrorMessage, EventLogEntryType.Error)
                End Try
    
            End Sub
    So, I do not know why I get a parser error from my json?
    Friday, October 30, 2015 9:16 AM
  • Hi HanneSThEGreaT,

    According to this case, As far as I know, maybe you can change the datatype as JSON not JSONP.

    As far as I know when we hosted WCF Service on different domain. we must use JSONP. JSONP,

    based on JSON, that allows pages to request data from a server in a different domain, normally

    disallowed.

    Also maybe you can set the following code to your web.config file. Like below:

    <webScriptEndpoint>
          <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
    </webScriptEndpoint>

    This node should be add as a child node to standardEndpoints

    Best Regards,

    Grady

    Tuesday, November 3, 2015 6:24 AM
    Moderator