gpt4 book ai didi

javascript - 如何从 URL 获取动态值

转载 作者:可可西里 更新时间:2023-11-01 08:44:54 25 4
gpt4 key购买 nike

我正在研究 AngularJS,我想根据 URL 值从 MySQL 加载内容。

我用的AngularJS RouteProvider是这样的:

$routeProvider.when('/page/pages=:pages', {
templateUrl: 'page.html',
reloadOnSearch: false
});

我的动态网址是:

"<a class='list-group-item' href='#/page/pages=" + slug + "'>" +
title + " <i class='fa fa-chevron-right pull-right'></i>" +
"</a>"

之后,我尝试在 PhoneGap 上提醒 URL 位置(附截图)

enter image description here

现在,我想将此 pages 值传递给 AJAX,以便从 MySQL 获得结果查询。

$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
jQuery.ajax({

url: 'http://keralapsctuts.com/app/index.php', //the script to call to get data
data: "pages="+pages, //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
type: "POST",
dataType: 'json', //data format
success:function(data) {
var result = "";
var title = "";

alert(pages);

for(var i=0; i < data.length; i++) {
var title = data[i]["pagetitle"]; //get name
var content = data[i]["pagecontent"]; //get slug
result += +content;
title += "<span>"+title+"</span>";
}

$('#pcontent').html(content); //Set output element html
$('#ptitle').html(title); //Set output element html
}
});
});

我没有得到任何输出。

最佳答案

要获取 URL 中的查询参数,请使用 AngularJS $location提供商。如果你的 url 看起来像 example.com/#/page?pages=5,你可以通过写

来获取值 5
 var urlPageValue = $location.search()["pages"];

确保通过编写在 Controller 中包含 $location 和 $http 服务

yourModuleName.controller("AwesomeController", ["$location", "$http", function($location, $http){
// Page initialization code (like your ajax call) here
}])

然后,按照 Szanto 的建议,使用 $http 服务通过编写进行 ajax 调用

$http({
url:"/yourApiEndpoint",
method: "POST",
data: { pages: urlPageValue }
}).success(function(response){
// Handle returned data here
}).error(function(){
// Handle errors here
})

最后,在您最初的问题中,当您从 SQL 调用中取回数据时,您手动将跨度附加到 DOM。如果你打算使用 AngularJS,你应该将响应分配给一个数组变量并使用 ngRepeat HTML 中的指令为数组中的每个项目实例化一些模板。

关于javascript - 如何从 URL 获取动态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com