Questo errore mi si è presentato in una applicazione SharePoint 2010 in cui c'era una chiamata JavaScript ad un servizio WCF tramite una chiamata jQuery AJAX.

Request Error
The server encountered an error processing the request. See server logs for more details
la chiamata era di questo tipo

JavaScript

var loginName = "SGART\user1";

var jsonParam = '{'
	+ '"loginName":"' + loginName + '"'
	+ '}';

$.ajax({
    type: "POST",
    url: "/helpdesk/_vti_bin/timesheetservice.svc/CdcAndCompany",
    data: jsonParam,
    processData: false,
    cache: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data, textStatus, jqXHR) {
        if (data.success == false) {
            var s = "";
            for (var ie = 0; ie < data.errors.length; ie++) {
                s += data.errors[ie];
            }
            lblCdc.html("Errors: " + er);
        } else {
            lblCdc.html(data.cdc);
            lblCompany.html(data.company);
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
        lblCdc.html(textStatus + " - " + errorThrown);
    }
});
ed andava sempre in errore. La variabile textStatus conteneva error e errorThrown conteneva Bad Request, errori molto generici.

Il problema era nella variabile loginName che conteneva una backslash ( \ ), una volta concatenata in jsonParam e passata al metodo dava errore.

Per risolverlo ho fatto l'escape del carattere backslash tramite il metodo replace:

JavaScript

var jsonParam = '{'
        + '"loginName":"' + loginName.replace("\\", "\\\\") + '"'
        + '}';

Lato server il WCF era questo:
<%@ ServiceHost Language="C#" Debug="true"
    Service="Sgart.HelpDesk.TimeSheetService, $SharePoint.Project.AssemblyFullName$"  
    CodeBehind="TimeSheetService.svc.cs"
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

C#: TimeSheetService.svc

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

[BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class TimeSheetService : ITimeSheet
{
       public UserInfo2 HelpDeskCdcAndCompany(string loginName)
        {
            UserInfo2 result = new UserInfo2();
            try
            {
                result.LoginName = loginName;
                string company="";
                string cdc = Manager.GetCDC(loginName, out company);

                result.CDC = cdc;
                result.Company = company;

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.AddError(ex);
            }
            return result;
        }
}

C#: ITimeSheet.cs

[ServiceContract]
    public interface ITimeSheet
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "CdcAndCompany"
            , BodyStyle = WebMessageBodyStyle.WrappedRequest
            , RequestFormat = WebMessageFormat.Json
            , ResponseFormat = WebMessageFormat.Json)]
        UserInfo2 HelpDeskCdcAndCompany(string loginName);

    }

    [DataContract]
    public class ServiceStatus
    {
        public ServiceStatus()
        {
            Success = false;
            Errors = new List<string>();
            ReturnValue = -1;
        }
        [DataMember(Name = "success")]
        public bool Success { get; set; }
        [DataMember(Name = "errors")]
        public List<string> Errors { get; set; }
        [DataMember(Name = "returnValue")]
        public int ReturnValue { get; set; }

        public void AddError(string message)
        {
            Errors.Add(message);
            Success = false;
        }
        public void AddError(Exception ex)
        {
            Errors.Add(ex.Message);
            Success = false;
        }
    }

    [DataContract]
    public class UserInfo2 : ServiceStatus
    {
        [DataMember(Name = "loginName")]
        public string LoginName;


        [DataMember(Name = "cdc")]
        public string CDC;

        [DataMember(Name = "company")]
        public string Company;
    }
Tags:
AJAX14 C#236 JQuery33 SharePoint498 SharePoint 2010224 WCF18
Potrebbe interessarti anche: