Skip to main content

Bug Tracker

Side navigation

#342 closed enhancement (invalid)

Opened November 02, 2006 09:40AM UTC

Closed December 19, 2006 06:59PM UTC

Last modified June 19, 2007 07:59AM UTC

spam

Reported by: mgibson@designlinks. 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.2.patch (0.6 KB) - added by mgibson@designlinks. November 07, 2006 05:07PM UTC.

    New patch against rev 540

  • ajax.patch (0.6 KB) - added by mgibson@designlinks. November 07, 2006 05:06PM UTC.

    New patch against rev 540

Change History (10)

Changed November 02, 2006 09:55AM UTC by mgibson@desi comment:1

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 )

Changed November 05, 2006 03:51PM UTC by Adrianna comment:2

cc: → Adrianna
component: ajax1
keywords: Content-TypeAdrianna
milestone: → 1
priority: minor1
summary: Set the Content-Type of an AJAX requestAdrianna
type: enhancement1
version: → 1

Changed November 07, 2006 05:03PM UTC by mgibson@desi comment:3

cc: Adrianna
component: → ajax
keywords: AdriannaContent-Type
priority: → major
summary: AdriannaSet the Content-Type of an AJAX request
type: → enhancement

Restored, after spam.

Changed November 07, 2006 05:09PM UTC by mgibson@desi comment:4

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!)

Changed November 07, 2006 05:16PM UTC by mgibson@desi comment:5

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!

Changed November 07, 2006 07:33PM UTC by anonymous comment:6

resolution: → fixed
status: newclosed

Changed December 18, 2006 11:01AM UTC by Alanna comment:7

resolution: fixed
status: closedreopened

Changed December 18, 2006 10:16PM UTC by joern comment:8

resolution: → invalid
status: reopenedclosed
summary: Set the Content-Type of an AJAX requestspam

Changed December 19, 2006 12:45PM UTC by Edgar comment:9

resolution: invalid
status: closedreopened

Changed December 19, 2006 06:59PM UTC by joern comment:10

resolution: → invalid
status: reopenedclosed