Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#342 closed enhancement (invalid)

spam

Reported by: mgibson@… Owned by:
Priority: major Milestone:
Component: ajax Version:
Keywords: Content-Type Cc:
Blocked by: Blocking:

Description

I'd like to suggest an additional option for $.ajax() to allow an alternative Content-Type, eg: application/json

I'd like to send raw JSON data to a PHP script, but PHP requires that a Content-Type other than the usual 'application/x-www-form-urlencoded' is set before it allows you to access the raw request data (via $HTTP_RAW_POST_DATA).

This will also allow the correct submission of XML, and other formats.

I've attached a patch against ajax.js

Cheers

  • Mark Gibson

Index: ajax.js =================================================================== --- ajax.js (revision 512) +++ ajax.js (working copy) @@ -686,6 +686,7 @@

var success = type.success; var error = type.error; var dataType = type.dataType;

+ var contentType = type.contentType
"application/x-www-form-urlencoded";

var global = typeof type.global == "boolean" ? type.global : true; var timeout = typeof type.timeout == "number" ? type.timeout : jQuery.timeout;

ifModified = type.ifModified
false;

@@ -708,7 +709,7 @@

Set the correct header, if data is being sent if ( data )

  • xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

+ xml.setRequestHeader("Content-Type", contentType);

Set the If-Modified-Since header, if ifModified mode. if ( ifModified )

Attachments (2)

ajax.patch (625 bytes) - added by mgibson@… 13 years ago.
New patch against rev 540
ajax.2.patch (625 bytes) - added by mgibson@… 13 years ago.
New patch against rev 540

Download all attachments as: .zip

Change History (12)

comment:1 Changed 13 years ago by mgibson@…

Sorry, wiki screwed up the patch, here it is again:

Index: ajax.js
===================================================================
--- ajax.js	(revision 512)
+++ ajax.js	(working copy)
@@ -686,6 +686,7 @@
 			var success = type.success;
 			var error = type.error;
 			var dataType = type.dataType;
+			var contentType = type.contentType || "application/x-www-form-urlencoded";
 			var global = typeof type.global == "boolean" ? type.global : true;
 			var timeout = typeof type.timeout == "number" ? type.timeout : jQuery.timeout;
 			ifModified = type.ifModified || false;
@@ -708,7 +709,7 @@
 		
 		// Set the correct header, if data is being sent
 		if ( data )
-			xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+			xml.setRequestHeader("Content-Type", contentType);
 		
 		// Set the If-Modified-Since header, if ifModified mode.
 		if ( ifModified )

comment:2 Changed 13 years ago by Adrianna

Cc: Adrianna added
Component: ajax1
Keywords: Adrianna added; Content-Type removed
Milestone: 1
Priority: minor1
Summary: Set the Content-Type of an AJAX requestAdrianna
Type: enhancement1
Version: 1

comment:3 Changed 13 years ago by mgibson@…

Cc: Adrianna removed
Component: ajax
Keywords: Content-Type added; Adrianna removed
Priority: major
Summary: AdriannaSet the Content-Type of an AJAX request
Type: enhancement

Restored, after spam.

Changed 13 years ago by mgibson@…

Attachment: ajax.patch added

New patch against rev 540

Changed 13 years ago by mgibson@…

Attachment: ajax.2.patch added

New patch against rev 540

comment:4 Changed 13 years ago by mgibson@…

New patch against rev 540:

Index: ajax.js
===================================================================
--- ajax.js	(revision 541)
+++ ajax.js	(working copy)
@@ -701,6 +701,7 @@
 			success: fvoid,
 			error: fvoid,
 			dataType: null,
+			contentType: "application/x-www-form-urlencoded",
 			data: null,
 			url: null
 		}, s);
@@ -736,7 +737,7 @@
 
 		// Set the correct header, if data is being sent
 		if ( s.data )
-			xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+			xml.setRequestHeader("Content-Type", contentType);
 
 		// Set the If-Modified-Since header, if ifModified mode.
 		if ( s.ifModified )

(Add attachment didn't seem to work!)

comment:5 Changed 13 years ago by mgibson@…

Sorry, slight typo in above patch, replace with this line:

xml.setRequestHeader("Content-Type", s.contentType);

I blame the spamming gits! They've really pissed me off!

comment:6 Changed 13 years ago by anonymous

Resolution: fixed
Status: newclosed

comment:7 Changed 13 years ago by Alanna

Resolution: fixed
Status: closedreopened

comment:8 Changed 13 years ago by joern

Resolution: invalid
Status: reopenedclosed
Summary: Set the Content-Type of an AJAX requestspam

comment:9 Changed 13 years ago by Edgar

Resolution: invalid
Status: closedreopened

comment:10 Changed 13 years ago by joern

Resolution: invalid
Status: reopenedclosed
Note: See TracTickets for help on using tickets.