问题描述
我继承了一些最终将成为 API 调用一部分的代码.基于现有代码,调用是一个使用 access_token 检索 JSON 代码的帖子.虽然这通常很简单,并且与其他所有 API 一样,但此代码需要为客户端密码提供自定义的 httpheader 字段.
I have inherited some code that will eventually be part of an API call. Based on the existing code, the call is a post to retrieve JSON code with an access_token. While this would normally be simple and like every other API out there, this code requires that there be a customized httpheader field for the client secret.
我能够使用 URLRequest 等在 Objective C 中完成这项工作,但现在我正在为 Web 组件创建调用,我遇到了障碍.
I was able to make this work in Objective C with URLRequest, etc. but now that I am creating the call for a web component, I have been roadblocked.
我正在使用一个非常标准的 jquery 帖子
I am using a pretty standard jquery post
$.post('https://url.com',
{access_token:'XXXXXXXXXXXXXXXXXXX',
function(data){
console.info(data);
}, 'json');
在标头中带有 HTTP-EQUIV.但是帖子从不检索数据,服务器本身也不会识别出任何调用(即使是不完整的调用).
With a HTTP-EQUIV in the header. But the post never retrieves data and the server itself doesn't recognized that any call was made (even an incomplete one).
我可能不得不废弃这段代码并重新开始,但如果有人以前遇到过这个问题,请提供任何见解.
I may have to scrap this code and start over, but if anyone has encountered this problem before, please offer any insight.
推荐答案
您发布的内容有语法错误,但没有区别,因为您无法通过 $.post()
传递 HTTP 标头.
What you posted has a syntax error, but it makes no difference as you cannot pass HTTP headers via $.post()
.
如果您使用的是 jQuery 版本 >= 1.5,请切换到 $.ajax()
并传递 headers
(docs) 选项.(如果您使用的是旧版本的 jQuery,我将通过 beforeSend
选项向您展示如何操作.)
Provided you're on jQuery version >= 1.5, switch to $.ajax()
and pass the headers
(docs) option. (If you're on an older version of jQuery, I will show you how to do it via the beforeSend
option.)
$.ajax({
url: 'https://url.com',
type: 'post',
data: {
access_token: 'XXXXXXXXXXXXXXXXXXX'
},
headers: {
Header_Name_One: 'Header Value One', //If your header name has spaces or any other char not appropriate
"Header Name Two": 'Header Value Two' //for object property name, use quoted notation shown in second
},
dataType: 'json',
success: function (data) {
console.info(data);
}
});
这篇关于带有自定义 HTTPHeader 字段的 JSON 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!