我的 REST API 开发人员告诉我,在转到实际链接之前,首先使用 OPTIONS
类型执行 ajax 调用,检查文件是否可供下载。
如果返回 OK,那么我可以继续将浏览器重定向到该页面并下载文件。
return $.ajax({
type: 'OPTIONS',
url: 'http://api/account-data',
data: {user_id: '5'}
}).done(function(data) {
// redirect browser to http://api/account-data?user_id=5
});
我遇到的问题是,当我使用OPTIONS
时,我无法附加任何查询字符串。另外,我以前从未听说过有人使用 OPTIONS
。据我所知,浏览器在执行某些跨源调用时会自动执行此操作。我告诉了他这一点,但我被告知要寻找附加查询字符串的解决方案。有办法吗?
最佳答案
Also, I've never heard of anyone using OPTIONS before.
人们通常不太擅长正确使用 HTTP。
也就是说,我希望使用 HEAD 请求来确定资源是否可用。
From what I've read, the browser does this automatically when doing certain cross-origin calls.
CORS 规范要求在某些情况下将 OPTIONS 请求作为飞行前请求,但 OPTIONS 早于 CORS,并且还有其他原因使用该 HTTP 动词。
I was told to search for a solution to attach the query strings. Is there a way?
当您发出 OPTIONS 请求时,jQuery 会忽略数据
。将查询字符串直接放入 URL 中。
$.ajax({
type: 'OPTIONS',
url: 'http://api/account-data?user_id=5'
})
关于javascript - 使用 $.ajax 类型选项发送查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613348/