How to set content-type header in Angular.JS Ajax Call

Having worked in Jquery and its Ajax for some years, I thought the the internal workings of Angular.JS Ajax call is similar because code looked very similar or no change but I didn’t get the result as expected. Let us see what was the problem and how to resolve it.

Jquery Ajax Call Code

image

Angular Ajax Call Code (Equivalent of the above)

image

After looked at the Http Header in the Chrome Inspector, I realised that the Content-Type header passed was missing

image

Following stack overflow provided me the solution, it said that “You need to include a body with the request. Angular removes the content-type header otherwise

image

Modified Angular Ajax Call Code (With dummy body i.e. data : ‘’)

image

After setting the body, Content-Type was passed and I got my results.

image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s