使用cookie从ajax post请求在交叉后续ajax get请求
Use cookie from ajax post request in cross subsequent ajax get request
这是我使用javascript的第一周,我试图通过post请求访问需要身份验证的api。后工作很好,但我不知道如何发送cookie与get请求。
EDIT2:
我将成功函数改为:
success: function(data, status, xhr);
更接近文档。从console.log(data, status, xhr)返回以下内容:
Object {readyState: 4, responseText: "{"value":{"publisher":{"active":"1","publisher_id":"######"}}}", responseJSON: Object, status: 201, statusText: "Created"}
也许这对外面的人来说澄清了一些事情?
下面是我的代码:<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script>
$(function() {
$.ajax({
url: "https://publisher-api.com/1.0/Publisher/Login",
type:"POST",
data: {"username":"######","password":"@@@@@@"},
dataType: "json",
success: function(response) {
console.log(response);
}
});
$.ajax({
url: "https://publisher-api.com/1.0/Publisher(######)/Channels/RevenueReport",
type: "GET",
dataType: "json",
success: function(data) {
console.log(data);
}
});
});
</script>
</head>
<body>
</body>
</html>
试试这个:
$.ajax({
url: "https://publisher-api.com/1.0/Publisher(######)/Channels/RevenueReport",
type: "GET",
dataType: "json",
xhrFields: {
withCredentials: true
},
success: function(data) {
console.log(data);
}
});
问题是ajax调用是异步的,所以当第一个调用仍在等待响应时,第二个调用也已经启动了。
如果我明白你想做什么(获取cookie与post,然后在get中使用cookie),你会想要嵌套ajax调用。所以
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script>
$(function() {
$.ajax({
url: "https://publisher-api.com/1.0/Publisher/Login",
type:"POST",
data: {"username":"######","password":"@@@@@@"},
dataType: "json",
success: function(response) {
$.ajax({
url: "https://publisher-api.com/1.0/Publisher("+ response.cookie +")/Channels/RevenueReport",
type: "GET",
dataType: "json",
success: function(data) {
console.log(data);
}
});
}
});
});
</script>
</head>
<body>
</body>
</html>
假设第一个ajax调用的响应包含cookie。
相关文章:
- ajax请求的顺序总是不同的
- 从ajax请求中获取javascript对象
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- JavaScript代码未正确检查ajax请求
- "日期“;AJAX请求返回的类型值未定义
- 在等待ajax请求时显示微调器并禁用页面
- 跨域ajax请求
- Ajax请求返回空的数据字符串,但首先得到了正确的数据
- Javascript-如何让脚本与Ajax请求的数据一起运行
- ajax请求成功,但可以'我看不到我的数据
- 如何将给定的curl命令复制为jquery ajax请求
- 为什么我能够从javascript控制台发送跨域ajax请求
- 一台特定计算机的Ajax请求数据未定义/失败
- 获取ajax请求的请求头
- 原型Ajax请求参数为嵌套json
- 可以't从AJAX请求中筛选数据
- jQuery Mobile Collapse上的AJAX请求
- 暂停函数执行流程,直到ajax请求完成