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

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

问题描述

我继承了一些最终将成为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()。

What you posted has a syntax error, but it makes no difference as you cannot pass HTTP headers via $.post().

如果您使用的是jQuery版本> = 1.5,请切换到 $。 ajax()并传递标题 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帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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