如何编码url以获取$ resource中的请求?

时间:2015-05-19 05:42:27

标签: javascript angularjs

这是我在控制器中的功能:

      event.preventDefault();
      url=event.target.href;                        
      var data=productionService.getUrl().get({
        urlvalue:encodeURIComponent(url)
      }, function(success){
        window.open(success,'_blank');
      }, function(error){
        alert("error:"+JSON.stringify(error))
      });
    } else {
      window.open(url,'_blank');
    }
  }
}

在工厂内:

getUrl:function(){
  return $resource("requesturl?id=:urlvalue");
}

点击网址时出现404错误,网址为http%25...而不是http:// 我该怎么做才能解决这个错误。

1 个答案:

答案 0 :(得分:-1)

因为您在http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp

中使用了encodeURIComponent
var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
var res = encodeURIComponent(uri);

res的结果将是:

http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab

你想要的是

getUrl:function(){
 return $resource(encodeURIComponent("requesturl?id=:urlvalue"));
}
相关问题