Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the Content-Type header using JavaScript

How can you set the Content-Type header to "application/x-www-form-urlencoded; charset=UTF-8" using JavaScript?

I need to do this so I can view a form with french characters without generating errors.

Thanks

like image 298
Bob K Avatar asked Jan 13 '10 17:01

Bob K


People also ask

How do I change the header in JavaScript?

Use the textContent property to change the text of a heading element, e.g. heading. textContent = 'Replacement heading text' . The textContent property will set the text of the heading to the provided string, replacing any of the existing content.

What is Content-Type header JS?

The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.

What is the Content-Type for JavaScript?

According to RFC 4329 the correct MIME type for JavaScript should be application/javascript .


2 Answers

Headers are set by the server delivering the content-type as part of the HTTP message. By the time it's in the browser and running the javascript it's too late. Do you have access to the server-side code? Why not set the content type to utf-8 in there? You can also do it as part of the meta tag in the head.

like image 103
Parrots Avatar answered Oct 13 '22 12:10

Parrots


You can add a meta tag into the head of the page, or send the header server-side.

example,

<meta http-equiv="Content-type" content="application/x-www-form-urlencoded; charset=UTF-8"/>

on the server-side, say PHP:

<?php
  header( 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' );
?>

that's it!

like image 22
Jacob Relkin Avatar answered Oct 13 '22 10:10

Jacob Relkin