Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#13758 closed bug (notabug)

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

Change History (8)

comment:1 Changed 10 years ago by KiranKumarT

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.

Last edited 10 years ago by KiranKumarT (previous) (diff)

comment:2 Changed 10 years ago by Rick Waldron

Owner: set to 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:

comment:3 in reply to:  2 Changed 10 years ago by KiranKumarT

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[[BR]] 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[[BR]] Content-Length: 9
Origin: http://localhost:14693[[BR]] 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',[[BR]] 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[[BR]] 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[[BR]] 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.

Last edited 10 years ago by KiranKumarT (previous) (diff)

comment:4 Changed 10 years ago by dmethvin

Resolution: notabug
Status: newclosed

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

comment:5 in reply to:  4 Changed 10 years ago by KiranKumarT

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

It is working fine in IE and Chrome

comment:6 in reply to:  4 Changed 10 years ago by KiranKumarT

Replying to 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!!!!

comment:7 Changed 10 years ago by dmethvin

Generates strange request (which I can see in Fiddler)

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

Origin: http://localhost:14693[[BR]]

You're making a cross-domain request against your own localhost, that's why the OPTIONS is occurring.

comment:8 in reply to:  7 Changed 10 years ago by KiranKumarT

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

Kiran.

Last edited 10 years ago by KiranKumarT (previous) (diff)
Note: See TracTickets for help on using tickets.