How can I pass request headers with jQuery's getJSON() method?

前端 未结 3 700
迷失自我
迷失自我 2020-12-04 17:07

I need to do a getJSON() request, but how do I pass authorisation and custom headers?

I am getting issues that the request header is taking the name, bu

3条回答
  •  粉色の甜心
    2020-12-04 18:08

    I agree with sunetos that you'll have to use the $.ajax function in order to pass request headers. In order to do that, you'll have to write a function for the beforeSend event handler, which is one of the $.ajax() options. Here's a quick sample on how to do that:

    
      
        
        
      
      
        

    Some Text

    If you run the code above and watch the traffic in a tool like Fiddler, you'll see two requests headers passed in:

    • securityCode with a value of Foo
    • passkey with a value of Bar

    The setHeader function could also be inline in the $.ajax options, but I wanted to call it out.

    Hope this helps!

提交回复
热议问题