Javascript jQuery Ajax POST请求
我试图获得以下代码,通过POST将变量发送到PHP页面。我不太清楚怎么做。这是我的代码,通过GET发送数据,通过JSON编码接收数据。要通过POST将变量传递到process_parts.php,我需要做哪些更改Javascript jQuery Ajax POST请求,javascript,jquery,ajax,post,request,Javascript,Jquery,Ajax,Post,Request,我试图获得以下代码,通过POST将变量发送到PHP页面。我不太清楚怎么做。这是我的代码,通过GET发送数据,通过JSON编码接收数据。要通过POST将变量传递到process_parts.php,我需要做哪些更改 function imagething(){ var done = false, offset = 0, limit = 20; if(!done) { var url = "process_parts.php?offset=" + of
function imagething(){
var done = false,
offset = 0,
limit = 20;
if(!done) {
var url = "process_parts.php?offset=" + offset + "&limit=" + limit;
$.ajax({
//async: false, defaults to async
url: url
}).done(function(response) {
if (response.processed !== limit) {
// asked to process 20, only processed <=19 - there aren't any more
done = true;
}
offset += response.processed;
$("#mybox").html("<span class=\"color_blue\">Processed a total of " + offset + " parts.</span>");
alert(response.table_row);
console.log(response);
imagething(); //<--------------------------recursive call
}).fail(function(jqXHR, textStatus) {
$("#mybox").html("Error after processing " + offset + " parts. Error: " + textStatus);
done = true;
});
}
}
imagething();
$.ajax({
url: "URL",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(ty),
dataType: "json",
success: function (response) {
alert(response);
},
error: function (x, e) {
alert('Failed');
alert(x.responseText);
alert(x.status);
}
});
函数imagething(){
var done=false,
偏移量=0,
限值=20;
如果(!完成){
var url=“process_parts.php?offset=“+offset+”&limit=“+limit;
$.ajax({
//async:false,默认为async
url:url
}).完成(功能(响应){
如果(response.processed!==限制){
//要求处理20,仅处理默认方法为GET
,若要更改此方法,请使用type
参数。您还可以将查询字符串属性作为对象提供,以便它们在URL中不立即可见:
var url = "process_parts.php";
$.ajax({
url: url,
type: 'POST',
data: {
offset: offset,
limit: limit
}
}).done(function() {
// rest of your code...
});
$.ajax({
url: "URL",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(ty),
dataType: "json",
success: function (response) {
alert(response);
},
error: function (x, e) {
alert('Failed');
alert(x.responseText);
alert(x.status);
}
});
试试这个
$.ajax({
url: "URL",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(ty),
dataType: "json",
success: function (response) {
alert(response);
},
error: function (x, e) {
alert('Failed');
alert(x.responseText);
alert(x.status);
}
});
一旦我添加了这些,我如何指定哪些post变量被传递到PHP?它们将在querystring中传递。我已经修改了我的答案,向您展示了如何将它们包含在post数据中。