我想获得URL变量(pid)到ajax请求(url)并没有取得任何成功。URL变量到ajax请求
我的网址是: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.');
}
});})
非常感谢您的帮助。
什么是'VAR ID = jQuery的(本).attr( 'PID');'应该在'$上下文做(文件).ready'回调? –
你试图获得'pid'属性的元素是什么?你用'this'来定义它,它意味着你选择你的整个html文档,因为你得到'document'对象 –
伙计我需要从url获得1256并将它插入到“$ .ajax({url:”,所以它会像 url:'http://www.domain.com/?post_type=news&post_id=1256, – qqruza