如何使用js或jQuery向ajax请求添加自定义HTTP标头 [英] How to add a custom HTTP header to ajax request with js or jQuery

查看:89
本文介绍了如何使用js或jQuery向ajax请求添加自定义HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何使用JavaScript或jQuery添加或创建自定义HTTP标头?

Does anyone know how to add or create a custom HTTP header using JavaScript or jQuery?

我正在使用下面的代码,但它给了我 405方法不允许
我正在使用 POST 方法,但在请求方法中,但它在请求方法中显示 OPTION
状态代码:405方法不允许,当我使用邮递员时它正在工作并向我显示200 ok状态代码。有没有人帮助我发生了什么问题。
i我在HTML中使用下面的代码。

i am using below code but it is giving me 405 Method not Allowed. i am using POST Method but in Request Method but its showing me OPTION in Request Method. Status Code:405 Method not Allowed, when i am using postman it is working and showing me 200 ok status code. does anyone help me what is the issue is going on. i am using below code in HTML.

$(document).ready(function () {
    $.ajax( {
        url: 'http://api.XXXXXXXXXX.com/SharedServices/SharedData.svc/rest/Authenticate',
        data: {
            "ClientId": "ApiIntegration",
            "UserName": "XXXXX",
            "Password": "XXXXX@123",
            "EndUserIp": "192.168.1.10"
        },
        method: 'POST',
        dataType: 'json',
        headers: {
            "contentType": "application/json",
            "cache-control": "no-cache"},

        success: function (response) {
            console.log(response);
        },
    });
})

如果我的代码中存在问题,那么jQuery代码是什么。
这里是我正在使用的邮递员女巫的截图。

what will be correct jQuery code, if there is an issue in my code. here is the screenshot of postman witch i am using.

推荐答案

jQuery代码中没有问题这是因为您提出了跨域请求。尝试以这种方式使用 https://www.youtube.com/watch?v=EPSjxg4Rzs8

there is no issue in jQuery code this is because you made a cross origin request. try to use in this way https://www.youtube.com/watch?v=EPSjxg4Rzs8

这篇关于如何使用js或jQuery向ajax请求添加自定义HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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