为什么$.ajax不和我一起工作?

为什么$.ajax不和我一起工作?

1)查找网络报头中的console.log错误或错误

2)确保在请求ajax的.php脚本中有允许访问控制的主要头文件。

3)有时,标头不允许您请求。

4)在发出请求时,请打开控制台(F12)并启用日志XMLhttpRequest

尝试将这些头添加到moremore.php中

代码语言:javascript复制header("Access-Control-Allow-Credentials: true");

header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");

header("Access-Control-Allow-Origin: http://localhost");另外,为了尝试一下,使用ajax头检查ajax请求,在moremore.php中也添加如下内容:

代码语言:javascript复制if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

echo '

alerts alerts by ajax
';

} else {

echo 'no ajax';

}这将使您了解ajax请求是否正常工作。

新ajax js:

代码语言:javascript复制$(document).ready(function() {

$('#btn').click(function() {

var ID = $(this).attr('id');

$(this).html("Loading ... ");

$.ajax({

type: "POST",

url: "http://localhost/my_projects/testing/moremore.php",

data: {

id: ID

},

cache: false,

xhrFields: {

withCredentials: true

},

dataType: html,

success: function(html) {

$('.container').append(html);

$('#btn').html("done");

},

error: function() {

alert("Something went wrong");

$("#btn").html("hmmmm, it was called... ");

}

});

});

});请留言寻求更多帮助!

相关推荐

唯品会面经〈说周日前发offer〉
365bet充值方式

唯品会面经〈说周日前发offer〉

02-09 👁️ 372
虫字旁的字
365bet最新备用网站

虫字旁的字

10-18 👁️ 2047