简体   繁体   中英

WCF service and AJAX call problems

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

We are writing a WCF wrapper 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:

var 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

在您的ajax调用中,尝试显式传递给带有参数名称的方法:

 data: {operationData: jsonRequestParameters},       // Data sent to server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM