JQuery Ajax Set Request header information Application/json

Source: Internet
Author: User

There's an API backstage in Application/json format today.
In jquery $.ajax default is contenttype:application/x-www-form-urlencoded; Charset=utf-8
This format is the form submission format, the data is K1=V1&K2=V2 format
Although you can set the {k1:v1,k2:v2} in data, it will be converted to k1=v1&k2=v2 at the time of submission.

So I added contenttype to the argument: ' Application/json;charset=utf-8 '.
But it's still an illegal request. 400
Finally, after a long time, I finally know why.
Data object needs to be converted to a JSON string

$.Ajax({  URL: ' Your URL ',  type: ' POST ',  DataType:"JSON",  ContentType: ' Application/json;charset=utf-8 ',  Data: JSON.stringify({ID: 1}),}). Done(function(data){  Console.Log(Data,"Success");})

This gives you a normal access to the

JQuery Ajax Set Request header information Application/json

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.