jQuery 带有自定义 HTTPHeader 字段的 JSON Post

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7100294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:23:54  来源:igfitidea点击:

JSON Post with Customized HTTPHeader Field

jqueryjsonposthttp-headers

提问by Izzy

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.

我继承了一些最终将成为 API 调用一部分的代码。基于现有代码,该调用是一个使用 access_token 检索 JSON 代码的帖子。虽然这通常很简单,并且与其他所有 API 一样,但此代码要求为客户端密钥提供自定义的 httpheader 字段。

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.

我能够使用 URLRequest 等在 Objective C 中完成这项工作,但是现在我正在创建对 Web 组件的调用,但我遇到了障碍。

I am using a pretty standard jquery post

我正在使用一个非常标准的 jquery 帖子

        $.post('https://url.com', 
        {access_token:'XXXXXXXXXXXXXXXXXXX',
         function(data){
           console.info(data);
         }, 'json');

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).

在标头中带有 HTTP-EQUIV。但是帖子从不检索数据,服务器本身也没有识别出任何调用(即使是不完整的调用)。

I may have to scrap this code and start over, but if anyone has encountered this problem before, please offer any insight.

我可能不得不废弃这段代码并重新开始,但如果有人以前遇到过这个问题,请提供任何见解。

回答by JAAulde

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

您发布的内容存在语法错误,但没有区别,因为您无法通过$.post().

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 beforeSendoption.)

如果您使用的是 jQuery 版本 >= 1.5,请切换到$.ajax()并传递headers( docs) 选项。(如果您使用的是较旧版本的 jQuery,我将通过beforeSend选项向您展示如何执行此操作。)

$.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);
    }
});

回答by Maulik Gangani

if one wants to use .post()then this will set headers for all future request made with jquery

如果想要使用 .post()那么这将为所有未来使用 jquery 发出的请求设置标头

$.ajaxSetup({
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
});

then make your .post() calls as normal.

然后像往常一样进行 .post() 调用。

回答by Sumit Singh

I tried as you mentioned, but only first parameter is going through and rest all are appearing in the server as undefined. I am passing JSONWebTokenas part of header.

我像你提到的那样尝试过,但只有第一个参数通过,其余的都在服务器中显示为undefined. 我JSONWebToken作为标题的一部分传递。

.ajax({
    url: 'api/outletadd',
    type: 'post',
    data: { outletname:outletname , addressA:addressA , addressB:addressB, city:city , postcode:postcode , state:state , country:country , menuid:menuid },
    headers: {
        authorization: storedJWT
    },
    dataType: 'json',
    success: function (data){
        alert("Outlet Created");
    },
    error: function (data){
        alert("Outlet Creation Failed, please try again.");        
    }

    });

回答by mriera

Just wanted to update this thread for future developers.

只是想为未来的开发人员更新此线程。

JQuery >1.12 Now supports being able to change every little piece of the request through JQuery.post ($.post({...}). see second function signature in https://api.jquery.com/jquery.post/

JQuery >1.12 现在支持能够通过 JQuery.post ($.post({...}) 更改请求的每一小块。参见https://api.jquery.com/jquery.post/ 中的第二个函数签名