Bug Tracker

Modify

Ticket #342 (closed enhancement: invalid)

Opened 7 years ago

Last modified 6 years ago

spam

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

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

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

Change History

comment:1 Changed 7 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 7 years ago by Adrianna

  • Cc Adrianna added
  • Component changed from ajax to 1
  • Summary changed from Set the Content-Type of an AJAX request to Adrianna
  • Priority changed from minor to 1
  • Version set to 1
  • Milestone set to 1
  • Keywords Adrianna added; Content-Type removed
  • Type changed from enhancement to 1

comment:3 Changed 7 years ago by mgibson@…

  • Cc Adrianna removed
  • Component set to ajax
  • Summary changed from Adrianna to Set the Content-Type of an AJAX request
  • Priority set to major
  • Keywords Content-Type added; Adrianna removed
  • Type set to enhancement

Restored, after spam.

Changed 7 years ago by mgibson@…

New patch against rev 540

Changed 7 years ago by mgibson@…

New patch against rev 540

comment:4 Changed 7 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 7 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 7 years ago by anonymous

  • Status changed from new to closed
  • Resolution set to fixed

comment:7 Changed 6 years ago by Alanna

  • Status changed from closed to reopened
  • Resolution fixed deleted

comment:8 Changed 6 years ago by joern

  • Status changed from reopened to closed
  • Resolution set to invalid
  • Summary changed from Set the Content-Type of an AJAX request to spam

comment:9 Changed 6 years ago by Edgar

  • Status changed from closed to reopened
  • Resolution invalid deleted

comment:10 Changed 6 years ago by joern

  • Status changed from reopened to closed
  • Resolution set to invalid

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.