[英]How do i send multiple requests for the same file and get the required response back in php using jquery ajax
var postID = $post->ID;
$.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri();?>/b.php",
data:{postID:postID},
dataType: 'json',
success: function(result){
if(result!=''){
r = $.parseJSON(result);
final_rating = get_final_rating(r);
set_stars(final_rating);
}
}
});
var arr = [a,b,c,d,e,f];
$.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri();?>/b.php",
data:{star:arr, postID:postID},
async :false,
cache: false,
success: function(result){
if(result === '1')
{
final_rating = result;
set_stars(final_rating);
}
}
});
You can do something like this with jQuery: 您可以使用jQuery执行以下操作:
var postID = <?php echo $post->ID; ?>,
arr = [a,b,c,d,e,f],
req1, req2;
req1 = $.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri();?>/b.php",
data: {postID:postID},
dataType: 'json'
});
req2 = $.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri();?>/b.php",
data: {star:arr, postID:postID},
async: false,
cache: false
});
$.when(req1, req2).then(function (data1, data2) {
// data1[0] = result
if(data1[0] !== '') {
r = $.parseJSON(result);
final_rating = get_final_rating(r);
set_stars(final_rating);
}
// data2[0] = result
if(data2[0] === '1') {
final_rating = result;
set_stars(final_rating);
}
});
var postID = $post->ID;
should be replaced by: 应替换为:
var postID = <?php echo $post->ID; ?>;
You're also doing Ajax wrong. 您也做错了Ajax。 You should make all requests on
admin-ajax.php
- http://codex.wordpress.org/AJAX_in_Plugins 您应该在
admin-ajax.php
- http: admin-ajax.php
上发出所有请求
You then use different action
parameters to differentiate between different Ajax calls. 然后,您可以使用不同的
action
参数来区分不同的Ajax调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.