URL变量转化为Ajax请求

I am trying to get the URL variable (pid) into the ajax request (url) and haven't got any success.

My url is: www.domain.com/news.html?pid=1256

My java script:

$(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.');
    }
});})

Thanks a lot for you help.

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');
   ...