Skip to main content

Bug Tracker

Side navigation

#13758 closed bug (notabug)

Opened April 09, 2013 03:05PM UTC

Closed April 09, 2013 05:06PM UTC

Last modified April 09, 2013 05:22PM UTC

Cannot set content-type to 'application/json' in jQuery.ajax

Reported by: KiranKumarT Owned by: KiranKumarT
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:
Description

I have the exactly same issue as shown in the below link

http://stackoverflow.com/questions/9754767/cannot-set-content-type-to-application-json-in-jquery-ajax

Please let me know what the issue is.

I am using JQuery Version 1.9.1

Attachments (0)
Change History (8)

Changed April 09, 2013 03:10PM UTC by KiranKumarT comment:1

_comment0: The situation exactly is: \ \ When I write code like below: \ \ $.ajax({ \ type: type, \ dataType: "json", \ url: url, \ data: parameter, \ processdata: true, \ crossDomain: true, \ async: false, \ success: function (result) { \ successFunction(result); \ }, \ error: function (a, b, c) { \ if (errorFunction) { \ errorFunction(a, b, c); \ } \ } \ }); \ \ I am getting Content-Type: application/x-www-form-urlencoded; charset=UTF-8 instead of expecting \ Content-Type: application/json; charset=UTF-8 \ \ I have tried with various options with processdata: true/false and crossDomain: true/false and also I remove these two and checked. \ \ Please advise. 1365526459455468

The situation exactly is:

When I write code like below:

$.ajax({

type: "POST",

dataType: "json",

url: http://localhost:xxxx/Service.svc/UpdatePassword,

data: parameters,

processdata: true,

crossDomain: true,

async: false,

success: function (result) {

successFunction(result);

},

error: function (a, b, c) {

if (errorFunction) {

errorFunction(a, b, c);

}

}

});

I am getting Content-Type: application/x-www-form-urlencoded; charset=UTF-8 instead of expecting

Content-Type: application/json; charset=UTF-8

I have tried with various options with processdata: true/false and crossDomain: true/false and also I remove these two and checked.

The above scenario is working fine in IE 8/9/10 and Chrome when we add Content-Type: application/json; charset=UTF-8 to the above code.

Please advise.

Changed April 09, 2013 03:22PM UTC by rwaldron comment:2

owner: → KiranKumarT
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket!

Additionally, be sure to test against the "jQuery (edge)" version to ensure the issue still exists—you may need to change this to a specific version depending on the test case.

To get you started, use the appropriate boilerplate:

Open the link and click to "Fork" (in the top menu) to begin.

Also, please read:

Additional resources:

Changed April 09, 2013 05:01PM UTC by KiranKumarT comment:3

_comment0: The link that I have provided do have fiddler information also. \ \ But for reference I am again providing it here. \ \ When I have this code \ \ $.ajax({ \ type: 'POST', \ //contentType: "application/json", \ url: 'http://localhost:16329/Hello', \ data: { name: 'norm' }, \ dataType: 'json' \ }); \ \ in Fiddler I can see following raw request \ \ POST http://localhost:16329/Hello HTTP/1.1 \ Host: localhost:16329 \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 \ Accept: application/json, text/javascript, */*; q=0.01 \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3 \ Accept-Encoding: gzip, deflate \ Connection: keep-alive \ Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \ Referer: http://localhost:14693/WebSite1/index.html \ Content-Length: 9 \ Origin: http://localhost:14693 \ Pragma: no-cache \ Cache-Control: no-cache \ \ name=Kiran \ \ But what I'm trying is to set content-type from application/x-www-form-urlencoded to application/json. But this code \ \ $.ajax({ \ type: "POST", \ contentType: "application/json", \ url: 'http://localhost:16329/Hello', \ data: { name: 'norm' }, \ dataType: "json" \ }); \ Generates strange request (which I can see in Fiddler) \ \ OPTIONS http://localhost:16329/Hello HTTP/1.1 \ Host: localhost:16329 \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 \ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3 \ Accept-Encoding: gzip, deflate \ Connection: keep-alive \ Origin: http://localhost:14693 \ Access-Control-Request-Method: POST \ Access-Control-Request-Headers: content-type \ Pragma: no-cache \ Cache-Control: no-cache \ \ In the first inputs If I get Content-Type: application/json; charset=UTF-8 instead of Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \ \ Then every thing will work properly.1365526987430486
_comment1: The link that I have provided do have fiddler information also. \ \ But for reference I am again providing it here. \ \ When I have this code \ \ $.ajax({ \ type: 'POST', \ //contentType: "application/json", \ url: 'http://localhost:16329/Hello', \ data: { name: 'norm' }, \ dataType: 'json' \ }); \ \ in Fiddler I can see following raw request \ \ POST http://localhost:16329/Hello HTTP/1.1 \ Host: localhost:16329 \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 \ Accept: application/json, text/javascript, */*; q=0.01 \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3 \ Accept-Encoding: gzip, deflate \ Connection: keep-alive \ Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \ Referer: http://localhost:14693/WebSite1/index.html \ Content-Length: 9 \ Origin: http://localhost:14693 \ Pragma: no-cache \ Cache-Control: no-cache \ \ name=Kiran \ \ But what I'm trying is to set content-type from application/x-www-form-urlencoded to application/json. But this code \ \ $.ajax({ \ type: "POST", \ contentType: "application/json", \ url: 'http://localhost:16329/Hello', \ data: { name: 'norm' }, \ dataType: "json" \ }); \ Generates strange request (which I can see in Fiddler) \ \ OPTIONS http://localhost:16329/Hello HTTP/1.1 \ Host: localhost:16329 \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 \ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3 \ Accept-Encoding: gzip, deflate \ Connection: keep-alive \ Origin: http://localhost:14693 \ Access-Control-Request-Method: POST \ Access-Control-Request-Headers: content-type \ Pragma: no-cache \ Cache-Control: no-cache \ \ In the first inputs If I get Content-Type: application/json; charset=UTF-8 instead of Content-Type: application/x-www-form-urlencoded; charset=UTF-8 \ \ Then every thing will work properly. \ \ By tomorrow Morning Indian Standard Time I will provide my Fiddler information. 1365527209964036
_comment2: The link that I have provided do have fiddler information also. \ \ But for reference I am again providing it here.[[BR]] \ \ When I have this code[[BR]] \ \ $.ajax({[[BR]] \ type: 'POST',[[BR]] \ //contentType: "application/json",[[BR]] \ url: 'http://localhost:16329/Hello',[[BR]] \ data: { name: 'norm' },[[BR]] \ dataType: 'json'[[BR]] \ });[[BR]] \ \ in Fiddler I can see following raw request[[BR]] \ \ POST http://localhost:16329/Hello HTTP/1.1[[BR]] \ Host: localhost:16329[[BR]] \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 \ Firefox/10.0.2[[BR]] \ Accept: application/json, text/javascript, */*; q=0.01[[BR]] \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3[[BR]] \ Accept-Encoding: gzip, deflate[[BR]] \ Connection: keep-alive[[BR]] \ Content-Type: application/x-www-form-urlencoded; charset=UTF-8[[BR]] \ Referer: http://localhost:14693/WebSite1/index.html[[BR]] \ Content-Length: 9[[BR]] \ Origin: http://localhost:14693[[BR]] \ Pragma: no-cache[[BR]] \ Cache-Control: no-cache[[BR]][[BR]] \ \ name=Kiran[[BR]][[BR]] \ \ But what I'm trying is to set content-type from application/x-www-form-urlencoded to application/json. But this code[[BR]] \ \ $.ajax({[[BR]] \ type: "POST",[[BR]] \ contentType: "application/json",[[BR]] \ url: 'http://localhost:16329/Hello',[[BR]] \ data: { name: 'norm' },[[BR]] \ dataType: "json"[[BR]] \ });[[BR]] \ Generates strange request (which I can see in Fiddler)[[BR]] \ \ \ OPTIONS http://localhost:16329/Hello HTTP/1.1[[BR]] \ Host: localhost:16329[[BR]] \ User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2[[BR]] \ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8[[BR]] \ Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3[[BR]] \ Accept-Encoding: gzip, deflate[[BR]] \ Connection: keep-alive[[BR]] \ Origin: http://localhost:14693[[BR]] \ Access-Control-Request-Method: POST[[BR]] \ Access-Control-Request-Headers: content-type[[BR]] \ Pragma: no-cache[[BR]] \ Cache-Control: no-cache[[BR]][[BR]] \ \ In the first inputs If I get Content-Type: application/json; charset=UTF-8 instead of Content-Type: application/x-www-form-urlencoded; charset=UTF-8[[BR]] \ \ Then every thing will work properly. \ \ By tomorrow Morning Indian Standard Time I will provide my Fiddler information. 1365527266854628
status: pendingnew

The link that I have provided do have fiddler information also.

But for reference I am again providing it here.

When I have this code

$.ajax({

type: 'POST',

//contentType: "application/json",

url: 'http://localhost:16329/Hello',

data: { name: 'norm' },

dataType: 'json'

});

in Fiddler I can see following raw request

POST http://localhost:16329/Hello HTTP/1.1

Host: localhost:16329

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101

Firefox/10.0.2

Accept: application/json, text/javascript, */*; q=0.01

Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Referer:

Pragma: no-cache

Cache-Control: no-cache

name=Kiran

But what I'm trying is to set content-type from application/x-www-form-urlencoded to application/json. But this code

$.ajax({

type: "POST",

contentType: "application/json",

url: 'http://localhost:16329/Hello',

data: { name: 'norm' },

dataType: "json"

});

Generates strange request (which I can see in Fiddler)

OPTIONS http://localhost:16329/Hello HTTP/1.1

Host: localhost:16329

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive

Origin:

Changed April 09, 2013 05:06PM UTC by dmethvin comment:4

resolution: → notabug
status: newclosed

This really does belong at StackOverflow, it's not a bug report it's a request for programming help.

Changed April 09, 2013 05:09PM UTC by KiranKumarT comment:5

No Its not a programming help. jQuery doesnot provide the correct Content-Type in Firefox.

It is working fine in IE and Chrome

Changed April 09, 2013 05:13PM UTC by KiranKumarT comment:6

Replying to [comment:4 dmethvin]:

This really does belong at StackOverflow, it's not a bug report it's a request for programming help.

Please look at the StackOverFlow link that I have provided.. It is also mention the issue with out solution.

It is working fine in IE and Chrome where as in Firefox the content-type is not provided correctly. Is this JQuery issue or Firefox issue!!!!

Changed April 09, 2013 05:15PM UTC by dmethvin comment:7

Generates strange request (which I can see in Fiddler)
OPTIONS http://localhost:16329/Hello HTTP/1.1
Origin:

Changed April 09, 2013 05:22PM UTC by KiranKumarT comment:8

_comment0: Ok Dmethvin. Thanks for your help. I will try keeping the code in live then will get back if needed. \ \ Kiran.1365528255038889

Ok dmethvin. Thanks for your help. I will try keeping the code in live then will get back if needed.

Kiran.