Javascript 未捕获错误:[$http:badreq]http请求配置url必须是字符串(AngularJS)
我正在开发一个webapp,遇到了一些困难,尤其是我很难摆脱的这个,下面是代码Javascript 未捕获错误:[$http:badreq]http请求配置url必须是字符串(AngularJS),javascript,c#,html,.net,angularjs,Javascript,C#,Html,.net,Angularjs,我正在开发一个webapp,遇到了一些困难,尤其是我很难摆脱的这个,下面是代码 this.bookSpace = function (date, spaceId) { swal({ title: "Are you sure?", text: "Would you like to book this space?", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55"
this.bookSpace = function (date, spaceId) {
swal({
title: "Are you sure?",
text: "Would you like to book this space?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, Book it!",
closeOnConfirm: false
},
function () {
var transform = function (data) {
return $.param(data);
};
$http.post(
{
url:"/api/api/AdhocBooking?bookingDate="+date.DateTime+"&spaceId="+spaceId,
headers: {'content-type': 'application/json; charset=UTF-8','Authorization': 'bearer'},
transformRequest: transform,
data: {
"Name": "Michael Knott"
}
});
swal("Booked!",
"Your space has been booked!",
"success");
})
这是我得到的答复
未捕获错误:[$http:badreq]http请求配置url必须为
一串
谢谢。首先,您需要将date.DateTime替换为encodeURIComponent(date.DateTime.toString())发布的代码中还有一点我看不出使用此函数的原因
function () {
var transform = function (data) {
return $.param(data);
};
因为您传递的数据已经是json
headers: {'content-type': 'application/json; charset=UTF-8','Authorization': 'bearer'},
transformRequest: transform,
data: {
"Name": "Michael Knott"
}
您可以在后台删除transformRequest函数,如果要使用它,您还需要在后端解码date.DateTime。
date.DateTime
或spaceId
不是字符串。如何在@Tushar执行此操作?它从HTML中检索日期,我会使用toString还是类似的东西?您的date.DateTime
可能是一个日期对象,您需要将其转换为字符串。查看js Date ref。我更改了Date.DateTime,如下所示。Date=Date.DateTime然后将url作为this.Date.toString()输入,但这不起作用。在添加DateTime后,您可以在此处发布url吗?你也可以试着用URL编码日期时间,然后附加它吗?如果这不起作用,那么您可以尝试在post参数中post datetime或将其设置为标头的一部分。