WCF服务和AJAX调用问题

本文关键字:调用 问题 AJAX 服务 WCF | 更新日期: 2023-09-27 18:00:11

很抱歉遇到编码问题,但想问一下是否有人知道。

我们正在编写一个WCF包装器服务来处理AJAX http请求。我提供了来自服务定义、服务web.config和客户端AJAX调用的代码片段。

问题是,当调用到达服务时,我们可以逐步通过断点,但由于某种原因,输入参数"operationData"为null?

我们无法判断问题是在web.config设置、服务定义还是AJAX调用语法中。


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;
    }
}

服务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输入到服务:

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 += "}";

对WCF服务的AJAX调用:

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);
}

我已经为此挣扎了两天了。如果有人能帮我解决这个问题,宇宙将以意想不到的方式回报。

感谢您的考虑!

热烈问候,

Joe

WCF服务和AJAX调用问题

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

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