CodeMan5000
CodeMan5000

Reputation: 1087

Bad Request - WCF with jQuery Post

I get a Bad Request Error 400 when I attempt to load Stats from my WCF Service. My call looks like so. I pulled out the date parameter to see if that was the cause, but no luck still getting the same error.

function WCFJSON() {
//var now = new Date();
//var getFromDate = dateToWcf(new Date(now - (60000 * 1440)));

var dt = new Date(now);
var dt1 = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
var getFromDate = dt1.toMSJSON();

var userid = "1";
m_Type = "POST";
m_Url = "https://dev-04.boldgroup.int/ManitouDashboard/DashboardProxyService.svc/GetStats"
m_Data = JSON.stringify({getFromDate: "'" + getFromDate + "'",getValueList: [1,2,3]});
m_DataType = "json";
m_ProcessData = true;             
CallService();
}

Date.prototype.toMSJSON = function () {
var date = '//Date(' + this.getTime() + ')//'; //CHANGED LINE
return date;
}; 

function CallService() {
$.ajax({
    type: m_Type,           //GET or POST or PUT or DELETE verb                  
    url: m_Url,                 //Location of the service   
    data: m_Data,
    dataType: m_DataType,   //Expected data format fserver                  
    processdata: m_ProcessData, //True or False
    crossdomain: true,    
    contentType: "application/json; charset=utf-8",             
    success: function (msg) {   //On Successfull service call                      
        ServiceSucceeded(msg);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        ServiceFailed("jqXHT: " + jqXHR.result + "Text Status: " + textStatus + " Error Thrown: " + errorThrown );
    } // When Service call fails              
});
}

The IDashboardWCFService Interface looks like this:

[ServiceContract]
public interface IDashboardWCFService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetStats", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    Dictionary<int,List<StatValue>> GetStats(DateTime getFromDate, List<int> getValueList);

    [OperationContract]
    [WebGet(UriTemplate = "GetStatTypes", ResponseFormat = WebMessageFormat.Json)]
    List<StatType> GetStatTypes();
}

Upvotes: 0

Views: 867

Answers (1)

Rajesh
Rajesh

Reputation: 7886

Seems like you are trying to perform a cross domain web service call via $ajax method. In that case your m_dataType value should be "jsonp" rather than "json".

Similar question here.

Upvotes: 1

Related Questions