WCF服务和AJAX调用问题

时间:2015-06-26 17:06:40

标签: c# ajax wcf

很抱歉打扰编码问题,但想过询问是否有人可能有线索。

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

问题是我们可以在呼叫到达服务时逐步通过断点,但输入参数" operationData"因某种原因而归零?

我们无法判断问题是在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 += "}";

AJAX调用WCF服务:

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

我一直在为此奋斗2天。如果有人能帮助我解决这个问题,那么宇宙将以意想不到的方式偿还。

感谢您的考虑!

热烈的问候,

1 个答案:

答案 0 :(得分:0)

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

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