问题描述:

I have a KnockoutJS viewmodel I'm working with, and sending a JSON date as a parameter in a GET request to WebAPI apparently is difficult.

I've got the following happening to my request

var params = {

userId: currentUser,

startRange: ko.toJSON(self.selectedDate()),

endRange: ko.toJSON(self.selectedDate())

}

$.ajax({

type: "GET",

contentType: 'application/json; charset=utf-8',

cache: false,

dataType: 'json',

url: url,

data: params,

beforeSend: function (xhr, settings) {

sf.setModuleHeaders(xhr);

}

}).done(function(data) {

}).fail(function () {

}).always(function() {

});

On my WebAPI controller, the Start and End dates keep coming up null

public HttpResponseMessage GetViolationsInRange(int userId, DateTime? startRange, DateTime? endRange)

Inspecting the request, this is what it outputs

/API/Cert/GetViolationsInRange?userId=20&startRange=%222015-09-01T04%3A00%3A00.000Z%22&endRange=%222015-09-01T04%3A00%3A00.000Z%22

Any ideas why this is occurring and why I keep getting tripped up by this?

EDIT: It seems that by not using jQuery's AJAX 'data' parameter, and just appending the values to the URL, it doesn't escape the JSON representations. Is there a clean way to do what jQuery does adding GET parameters without manually appending them?

网友答案:

try removing the use of ko.toJSON(). The date will be a property of your params object.

Jquery with dataType : 'json' will translate the date object properly.

相关阅读:
Top