带有自定义 HTTPHeader 字段的 JSON Post [英] JSON Post with Customized HTTPHeader Field

查看:22
本文介绍了带有自定义 HTTPHeader 字段的 JSON Post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些最终将成为 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 Post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆