Javascript Ajax请求取决于第一个(然后)

Javascript Ajax请求取决于第一个(然后),javascript,php,ajax,Javascript,Php,Ajax,我试图使用第一个函数来检查该项是否存在,以及是否可以将该项添加到PHP$\u会话 我希望在$\会话中最多有3个项目,如果该项目存在于会话中,它将返回1。以便收藏夹函数可以更新会话中属于项目ID的信息。$\u会话有一个多维数组,其中包含项目id和项目属性数组。稍后,当侧栏打开时,我有一个ajax调用请求会话中的所有相关信息,然后查询到我的SQL以获取最新信息。PHP然后清理代码并将其作为JSON发送回,Javascript对其进行解析以将其放在侧栏中 我一直在试图弄清楚如何使用JavaScript

我试图使用第一个函数来检查该项是否存在,以及是否可以将该项添加到PHP$\u会话

我希望在$\会话中最多有3个项目,如果该项目存在于会话中,它将返回1。以便收藏夹函数可以更新会话中属于项目ID的信息。$\u会话有一个多维数组,其中包含项目id和项目属性数组。稍后,当侧栏打开时,我有一个ajax调用请求会话中的所有相关信息,然后查询到我的SQL以获取最新信息。PHP然后清理代码并将其作为JSON发送回,Javascript对其进行解析以将其放在侧栏中

我一直在试图弄清楚如何使用JavaScript中的“.then”选项来实现这一点,但我没有进一步了解它

(我在代码中添加了一些注释来解释它的功能以及它应该从PHP/$\u会话中得到什么)


因此,我们一直在研究一些示例获取代码(仍然没有完全清除所有内容),但这会变成这样吗

var request = new Request('/nr_check.php',{
 method: 'POST',
 mode: 'cors', //what is this and do i need it?
 redirect: 'follow',// same
 headers: new Headers({
     'Content-Type': 'text/plain'
     'X-Requested-With', 'XMLHtppRequest' //is this still necessary? 
 })
});

 xhr.send("door=" + door_param); // where those this go now?

 fetch(request).then(function(favorite){
     if(request.result <= 3){
        favorite();
     } else {
          sbfSwitch();
     }
 });
var request=新请求('/nr_check.php'{
方法:“POST”,
mode:'cors',//这是什么,我需要它吗?
重定向:“follow',//相同
标题:新标题({
“内容类型”:“文本/普通”
“X-request-With”“XMLHtppRequest”“//仍然需要这样做吗?
})
});
xhr.send(“门=”+门参数);//这些东西现在去哪里了?
获取(请求)。然后(函数(收藏夹){

如果(request.result使用
fetch
您的前两个函数

function favoriteNr() {
    var body = new FormData();
    body.append('door', gup('name'));
    return fetch('/nr_check.php', { 
        method: 'POST',
        headers: new Headers({
            'Content-type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHtppRequest'
        }),
        body: body
    })
    .then(function(response) {
        if (!response.ok) {
            throw new Error('Network status ' + response.status + ':' + response.statusText)
        }
        return response.text();
    })

function favorite() {
    var parent = this.parentElement;
    var body = new FormData();
    body.append('door', gup('name'));
    body.append('color', parent.id);
    return fetch('/favorite.php', { 
        method: 'POST',
        headers: new Headers({
            'Content-type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHtppRequest'
        }),
        body: body
    })
    .then(function(response) {
        if (!response.ok) {
            throw new Error('Network status ' + response.status + ':' + response.statusText)
        }
        return response.text();
    })
    .then(function(result) {
        if(result == 'color') {
            parent.classList.remove("favorite");
        }
    });
}

favoriteNr()
.then(result) {
    var n = parseInt(result);
    if (n >=0 && n <=2) {
        return favorite();
    }
    sbfSwitch();
}
函数favoriteNr(){
var body=new FormData();
body.append('door',gup('name'));
返回fetch('/nr_check.php',{
方法:“POST”,
标题:新标题({
“内容类型”:“应用程序/x-www-form-urlencoded”,
“X-request-With':“XMLHtppRequest”
}),
身体:身体
})
.然后(功能(响应){
如果(!response.ok){
抛出新错误('networkstatus'+response.status+':'+response.statusText)
}
返回response.text();
})
函数favorite(){
var parent=this.parentElement;
var body=new FormData();
body.append('door',gup('name'));
body.append('color',parent.id);
返回fetch('/favorite.php',{
方法:“POST”,
标题:新标题({
“内容类型”:“应用程序/x-www-form-urlencoded”,
“X-request-With':“XMLHtppRequest”
}),
身体:身体
})
.然后(功能(响应){
如果(!response.ok){
抛出新错误('networkstatus'+response.status+':'+response.statusText)
}
返回response.text();
})
.然后(函数(结果){
如果(结果=‘颜色’){
parent.classList.remove(“收藏夹”);
}
});
}
favoriteNr()
.然后(结果){
var n=parseInt(结果);

如果(n>=0&&n
如何处理“.then”JavaScript中的选项
-好吧,你没有使用承诺,而且绝对没有
。然后在你发布的代码中
。然后
不是一个JavaScript选项,它是一个
承诺的方法
好吧,那么我怎么能用承诺来写呢?试着使用
获取
:p如果失败了,你最好的办法就是写some代码“包装”Promise中的xmlhttprequest无法使用代码块进行回答,因此在answers中编写了它,正在尝试您的建议,并希望得到一些建议
这些建议现在在哪里
…在请求对象
主体
属性
模式:cors
-您的请求是否跨来源?
内容类型:'text/plain'
-为什么?使用您的original contentThx对于所有这些信息,明天将进一步查看。我是JavaScript新手,在你提到fetch之前,我还没有听说过它,所以我只是在尝试我现在能找到的东西。我主要用php或css解决我的问题。你看过我发布的答案了吗?它有效吗(我只有90%的信心:p)它们现在仍然是独立的函数,对吗?所以如果我调用favorite,我只会得到favorite的结果。这就是为什么我想让favorite知道favoriteNr()的结果。这将阻止它仅向我的PHPY提供数据…它们是…根据您的代码。让我再次阅读问题,我一定错过了关于
获取
-我认为它不会在请求中发送cookies感谢您的帮助!今天早上我在看我自己的问题,并说我为什么不这样做他的方法?php可以解决这个更干净、更少冗余的问题。我确实喜欢“then/fetch”的东西,但它还没有得到广泛的支持。对我的函数做了一个小小的更改if(result='door'){parent.classList.add(“favorite”);}if(result>=3){sbfSwitch();}用php javascript的结果完成我想要的工作
function favoriteNr() {
    var body = new FormData();
    body.append('door', gup('name'));
    return fetch('/nr_check.php', { 
        method: 'POST',
        headers: new Headers({
            'Content-type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHtppRequest'
        }),
        body: body
    })
    .then(function(response) {
        if (!response.ok) {
            throw new Error('Network status ' + response.status + ':' + response.statusText)
        }
        return response.text();
    })

function favorite() {
    var parent = this.parentElement;
    var body = new FormData();
    body.append('door', gup('name'));
    body.append('color', parent.id);
    return fetch('/favorite.php', { 
        method: 'POST',
        headers: new Headers({
            'Content-type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHtppRequest'
        }),
        body: body
    })
    .then(function(response) {
        if (!response.ok) {
            throw new Error('Network status ' + response.status + ':' + response.statusText)
        }
        return response.text();
    })
    .then(function(result) {
        if(result == 'color') {
            parent.classList.remove("favorite");
        }
    });
}

favoriteNr()
.then(result) {
    var n = parseInt(result);
    if (n >=0 && n <=2) {
        return favorite();
    }
    sbfSwitch();
}