0

我正在尝试将 URL 变量(pid)放入 ajax 请求(url)中,但没有成功。

我的网址是:www.domain.com/news.html?pid=1256

我的java脚本:

$(document).ready(function() {
var output = $('#news');
var id = jQuery(this).attr('pid');

$.ajax({
    url: 'http://www.domain.com/?post_type=news&post_id=' + id,
    async: false,
    callback: 'callback',
    crossDomain: true,
    contentType: 'application/json; charset=utf-8',
    type: 'POST',
    dataType: 'jsonp',
    timeout: 5000,
    success: function(data, status) {
        $.each(data.posts, function(i, item) {
            var news = '<div>' + item.title + '</div><div>' + item.content + '</div><hr/>';

            output.append(news);


        });
    },
    error: function() {
        output.text('There was an error loading the data.');
    }
});})

非常感谢你的帮助。

4

1 回答 1

2

If I understand correctly, you want to get the query param 'pid' from your current page?

You can get the query params via window.location.search.

To get a specific param, you should create a getQueryVariable() function.

So in your case:

var getQueryVariable = function(variable) {
   ...
};
$(document).ready(function() {
   var output = $('#news');    
   var id = getQueryVariable('pid');
   ...
于 2012-12-27T10:19:54.383 回答