如何解析以下URL查询字符串以获取数量参数的值?
myurl / Channels / BuyCredits?quantity = 2
我尝试了无数成功的例子:
我只是试过这个例子:
marvmentApp.controller("channelCreditController", function($scope, $http, $compile, $location) {
var params, test2;
$scope.parseQueryString = function() {
var objURL, str;
str = window.location.search;
objURL = {};
str.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"), function($0, $1, $2, $3) {});
objURL[$1] = $3;
return;
return objURL;
};
params = $scope.parseQueryString();
test2 = params['quantity'];
debugger;
});
我不断收到错误$ 1的定义
使用JavaScriptlocation.search
属性。它将返回您的URL的查询字符串(包括“?”符号)。
如果URL是 http://www.example.org/index.php?param=arg
值location.search
是?param=arg
。
要删除“?”,请使用JS replace方法:
var url = "?param=arg"; // pretend that the variable "url" is the query string
url = url.replace(/[\?]/,"");
document.getElementById('output').innerHTML = "Query string is: \"" + url + "\"";
<p id="output"></p>
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句