none
WCF service and AJAX calls RRS feed

  • Question

  • Hi All,

    Sorry to bother with a coding issue but thought to ask to see if anyone might have a clue.

    We are writing a WCF service to process AJAX http requests. I am providing code snippets from the service definition, service web.config and client-side AJAX call.

    The problem is we can step through breakpoints when the call hits the service, yet the input parameter "operationData" is coming in null for some reason?

    We can't tell if the problem is in the web.config settings, the service definition or the AJAX call syntax.

    -------------------------------------------------------------------------------

    Service definition:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    using System.Text;

    [ServiceContract(Namespace = "localhost")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ST1Services
    {
        [OperationContract]
        // As soon as we include this attribute, the service never gets hit???
        //[WebInvoke(
        //    UriTemplate = "/stapiRegisterPlayer/{operationData}",
        //    RequestFormat = WebMessageFormat.Json,
        //    ResponseFormat = WebMessageFormat.Json,
        //    Method = "POST")
        //]
        public string stapiRegisterPlayer(string operationData)
        {
            var serviceResponse = "{\"servicesresponse\":\"Successfull!\"}";

            return serviceResponse;
        }
    }


    -------------------------------------------------------------------------------

    Service web.config:
      <system.serviceModel>
        
        <behaviors>
          <endpointBehaviors>
            <behavior name="ST1ServicesAspNetAjaxBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
        
        <services>
          <service name="ST1Services">
            <endpoint address="" behaviorConfiguration="ST1ServicesAspNetAjaxBehavior" binding="webHttpBinding" contract="ST1Services" />
          </service>
        </services>
        
      </system.serviceModel>

    -------------------------------------------------------------------------------

    JSON input to service:

    jsonRequestParameters = "{";
    jsonRequestParameters += "  'operatorId': '188',";
    jsonRequestParameters += "  'siteId': '229',";
    jsonRequestParameters += "  'siteUsername': '1886232015229',";
    jsonRequestParameters += "  'sitePwd': '1H#k9rr4ocReKfSt',";
    jsonRequestParameters += "  'geoComplyEncryptedPacket': 'ZsUiDymAiyVr/aQxwqC60c50qCfhJ9WPvZo3TrNAmXxD20onJILaqkmK+CGEDzr7tveVE=',";
    jsonRequestParameters += "  'userName': 'tuser-20154825104',";
    jsonRequestParameters += "  'ipAddress': '184.182.215.167',";
    jsonRequestParameters += "  'playerDetails': ";
    jsonRequestParameters += "  {";
    jsonRequestParameters += "      'userName': 'tuser-20154825104',";
    jsonRequestParameters += "      'firstName': 'Test',";
    jsonRequestParameters += "      'middleInitial': 'J',";
    jsonRequestParameters += "      'lastName': 'User',";
    jsonRequestParameters += "      'gender': 'Male',";
    jsonRequestParameters += "      'dob': '07/1/1972',";
    jsonRequestParameters += "      'emailAddress': 'test@user.com',";
    jsonRequestParameters += "      'playerAddress1': '7275 E Gold Dust',";
    jsonRequestParameters += "      'playerAddress2': '#224',";
    jsonRequestParameters += "      'city': 'Paradise Valley',";
    jsonRequestParameters += "      'county': 'Maricopa',";
    jsonRequestParameters += "      'state': 'Arizona',";
    jsonRequestParameters += "      'zipCode': '85258',";
    jsonRequestParameters += "      'country': 'United States',";
    jsonRequestParameters += "      'mobileNo': '+1-602-555-1212',";
    jsonRequestParameters += "      'landLineNo': '+1-602-555-1212',";
    jsonRequestParameters += "      'ssn': '111-22-3333',";
    jsonRequestParameters += "      'dlNumber': 'D08019649',";
    jsonRequestParameters += "      'dlIssuingState': 'Arizona',";
    jsonRequestParameters += "      'ipAddress': '184.182.215.167'";
    jsonRequestParameters += "    }";
    jsonRequestParameters += "}";

    -------------------------------------------------------------------------------

    AJAX call to WCF service:

        var serviceURL = "http://localhost/STI/webservices/ST1Services.svc/stapiRegisterPlayer";

        try
        {
            $.ajax({
                type: "POST",                 // GET or POST or PUT or DELETE verb
                url: serviceURL,                 // Location of the service
                data: jsonRequestParameters,         // Data sent to server
                contentType: "application/json;",         //"application/json; charset=utf-8;", // content type sent to server
                dataType: "json",                 // Expected data format from server
                processdata: false,                // True or False
                success: function (msg) {             //On Successfull service call
                    ServiceSucceeded(msg);
                },
                error: ServiceFailed             // When Service call fails
            });
        }
        catch(err)
        {
            alert(err.message);
        }

    I have been struggling with this for 2 days. If anyone can help me solve this problem, the universe will repay in unexpected ways.

    Thank you for your consideration!

    Warm regards,

    Joe

    Friday, June 26, 2015 6:20 PM

Answers

  • Hi Mactd,

    According to your description, in my opinion is that you can deal with issues that cross-domain in your configuration file.Then, you can  modify your code to your 'IService' files.Next,you can check the field properties that you use and match property of a field in the database , for example , if you allowed null and your field is char not nvarchar . So sometimes will appear error.  

    ====================================

     1). cross-domain in your configuration file:

    <bindings>
          <webHttpBinding>
            <binding crossDomainScriptAccessEnabled="true"></binding>
          </webHttpBinding>
    </bindings>
    This node should be added as a child of the serviceModel node ( <system.serviceModel>).
     <endpointBehaviors>
          <behavior name="web">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
    This node should be added as a child of the behaviors node (<behaviors>).
    <standardEndpoints>
        <webScriptEndpoint>
          <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
        </webScriptEndpoint>
      </standardEndpoints>
    This node should be added as a child of the serviceModel node ( <system.serviceModel>).
     2). modify your code to your IService files:
    May be like this:
    [WebInvoke(
            UriTemplate = "/stapiRegisterPlayer/",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            Method = "POST")
         ]

    #WCF REST Services Using jQuery AJAX Calls

    http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call

    ====================================
     I hope that will be helpful to you.

    Best Regards,

    Tracy Dj


    Tuesday, June 30, 2015 5:01 AM
    Moderator

All replies

  •  

    url: serviceURL,                 // Location of the service

    It seem to me that the parms should be coming in the URI or URL like in examples of a Web API Method having parms passed into it.

    https://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/

    It could be that you may be missing a  helper DLL   that the Post or Get method uses that needs parms passed into it some kind of  a Jason helper object.

    MVC vs WebAPI Parameter Binding

    http://www.telerik.com/blogs/the_facts_on_using_kendo_ui_with_asp_net_webapi

    You should look at the example code. I am not saying it is an example you should use, because it has nothing to so with what you are doing. But it should raise the question to you with your Jason and Ajax stuff. Are you missing a helper that is going to get the parms passed into the service method?

    Friday, June 26, 2015 7:38 PM
  • Hi Mactd,

    According to your description, in my opinion is that you can deal with issues that cross-domain in your configuration file.Then, you can  modify your code to your 'IService' files.Next,you can check the field properties that you use and match property of a field in the database , for example , if you allowed null and your field is char not nvarchar . So sometimes will appear error.  

    ====================================

     1). cross-domain in your configuration file:

    <bindings>
          <webHttpBinding>
            <binding crossDomainScriptAccessEnabled="true"></binding>
          </webHttpBinding>
    </bindings>
    This node should be added as a child of the serviceModel node ( <system.serviceModel>).
     <endpointBehaviors>
          <behavior name="web">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
    This node should be added as a child of the behaviors node (<behaviors>).
    <standardEndpoints>
        <webScriptEndpoint>
          <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint>
        </webScriptEndpoint>
      </standardEndpoints>
    This node should be added as a child of the serviceModel node ( <system.serviceModel>).
     2). modify your code to your IService files:
    May be like this:
    [WebInvoke(
            UriTemplate = "/stapiRegisterPlayer/",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            Method = "POST")
         ]

    #WCF REST Services Using jQuery AJAX Calls

    http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call

    ====================================
     I hope that will be helpful to you.

    Best Regards,

    Tracy Dj


    Tuesday, June 30, 2015 5:01 AM
    Moderator