Javascript JQuery/ajax页面更新帮助请
您好,我是Jquery/ajax新手,需要最后(我想)一段代码的帮助。 我有一个draggable项(JQuery ui.draggable),当放置在drop区域中时,它会更新mysql表-这是有效的,如下所示:Javascript JQuery/ajax页面更新帮助请,javascript,jquery,ajax,post,Javascript,Jquery,Ajax,Post,您好,我是Jquery/ajax新手,需要最后(我想)一段代码的帮助。 我有一个draggable项(JQuery ui.draggable),当放置在drop区域中时,它会更新mysql表-这是有效的,如下所示: function addlist(param) { $.ajax({ type: "POST", url: "ajax/addtocart.php", data: 'img='+encodeURIComponent(param), da
function addlist(param)
{
$.ajax({
type: "POST",
url: "ajax/addtocart.php",
data: 'img='+encodeURIComponent(param),
dataType: 'json',
beforeSend: function(x){$('#ajax-loader').css('visibility','visible');}
});
}
但我无法让它做的是“重新加载”另一页/同一页以显示更新的结果。
简单地说,我想
我不知道PHP,但您需要的是addtocart.PHP来返回某种响应(echo?) 你会照顾的
$.ajax({
type: "POST",
url: "ajax/addtocart.php",
data: 'img='+encodeURIComponent(param),
dataType: 'json',
beforeSend: function(x){$('#ajax-loader').css('visibility','visible');
success: function(response){ /* use the response to update your html*/} });
我不知道PHP,但您需要的是addtocart.PHP来返回某种响应(echo?) 你会照顾的
$.ajax({
type: "POST",
url: "ajax/addtocart.php",
data: 'img='+encodeURIComponent(param),
dataType: 'json',
beforeSend: function(x){$('#ajax-loader').css('visibility','visible');
success: function(response){ /* use the response to update your html*/} });
做这件事的方法有很多。我可能会让PHP脚本输出需要显示的内容。这可以通过JSON(基本上是用JavaScript语法编码的数据)或原始HTML来实现 如果要使用原始HTML:
function addlist(param)
{
$.ajax(
{
type: 'POST',
url: 'ajax/addtocart.php',
data: 'img=' + encodeURIComponent(param),
dataType: 'html',
beforeSend: function()
{
$('#ajax-loader').css('visibility','visible');
},
success: function(data, status)
{
// Process the returned HTML and append it to some part of the page.
var elements = $(data);
$('#some-element').append(elements);
},
error: function()
{
// Handle errors here.
},
complete: function()
{
// Hide the loading GIF.
}
});
}
如果使用JSON,过程基本上是相同的,只是您必须自己在JavaScript中构造新的HTML元素(显然,PHP脚本的输出必须使用JSON\u encode
进行编码)。您的成功
回调可能如下所示:
function(data, status)
{
// Get some properties from the JSON structure and build a list item.
var item = $('<li />');
$('<div id="div-1" />').text(data.foo).appendTo(item);
$('<div id="div-2" />').text(data.bar).appendTo(item);
// Append the item to some other element that already exists.
$('#some-element').append(item);
}
功能(数据、状态)
{
//从JSON结构中获取一些属性并构建一个列表项。
变量项=$(“”);
$('').text(data.foo).appendTo(item);
$('').text(data.bar).appendTo(item);
//将该项附加到其他已存在的元素。
$(“#某些元素”)。追加(项目);
}
有很多方法可以做到这一点。我可能会让PHP脚本输出需要显示的内容。这可以通过JSON(基本上是用JavaScript语法编码的数据)或原始HTML来实现
如果要使用原始HTML:
function addlist(param)
{
$.ajax(
{
type: 'POST',
url: 'ajax/addtocart.php',
data: 'img=' + encodeURIComponent(param),
dataType: 'html',
beforeSend: function()
{
$('#ajax-loader').css('visibility','visible');
},
success: function(data, status)
{
// Process the returned HTML and append it to some part of the page.
var elements = $(data);
$('#some-element').append(elements);
},
error: function()
{
// Handle errors here.
},
complete: function()
{
// Hide the loading GIF.
}
});
}
如果使用JSON,过程基本上是相同的,只是您必须自己在JavaScript中构造新的HTML元素(显然,PHP脚本的输出必须使用JSON\u encode
进行编码)。您的成功
回调可能如下所示:
function(data, status)
{
// Get some properties from the JSON structure and build a list item.
var item = $('<li />');
$('<div id="div-1" />').text(data.foo).appendTo(item);
$('<div id="div-2" />').text(data.bar).appendTo(item);
// Append the item to some other element that already exists.
$('#some-element').append(item);
}
功能(数据、状态)
{
//从JSON结构中获取一些属性并构建一个列表项。
变量项=$(“”);
$('').text(data.foo).appendTo(item);
$('').text(data.bar).appendTo(item);
//将该项附加到其他已存在的元素。
$(“#某些元素”)。追加(项目);
}