未捕获的错误:[$ http:badreq] Http请求配置url必须是一个字符串(AngularJS)

时间:2016-07-13 10:39:30

标签: 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",
    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请求配置网址必须是   一个字符串

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要将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。