Permalink
Browse files

Merge pull request #171 from nicolas-rempulski/master

Query string not build properly on Ajax Request (apart from GET method)
  • Loading branch information...
unwiredben committed Nov 15, 2012
2 parents e877f13 + 6f5649f commit 82bbf229ec915eb4a39bb1c6cad976e4a9e3a208
Showing with 48 additions and 9 deletions.
  1. +25 −2 source/ajax/Ajax.js
  2. +1 −0 source/ajax/AjaxProperties.js
  3. +7 −2 tools/test/ajax/php/test2.php
  4. +15 −5 tools/test/ajax/tests/AjaxTest.js
View
@@ -31,7 +31,18 @@ enyo.kind({
},
//* @public
/**
- Sends the ajax request with parameters _inParams_.
+ Sends the ajax request with parameters _inParams_. _inParams_ values can be either a **String** or an **Object**.
+
+ _inParams_ as an **Object** is converted into the url query string. For instance passing <code>{q: "searchTerm"}</code>
+ would result in the addition of the string **q="searchTerm"** to the current url query string.
+
+ _inParams_ as a **String** is used as the request body and trigger various behaviors depending on query **method**.
+ - GET method, as it has no body, translates it to a parameter. Thus <code>'q="searchTerm"'</code> translates into
+ **q="searchTerm** in the url query string.
+ - POST method uses the provided string as its body. But this one **will be overrided** by postBody value if it is set.
+
+ Use of _inParams_ as **String** is discouraged. Set **postBody** value to ensure the request body content and _inParams_
+ as an **Object** to set the query string.
*/
go: function(inParams) {
this.startTimer();
@@ -44,7 +55,19 @@ enyo.kind({
var uri = parts.shift() || "";
var args = parts.length ? (parts.join("?").split("&")) : [];
//
- var body = enyo.isString(inParams) ? inParams : enyo.Ajax.objectToQuery(inParams);
+ var body = null;
+ //
+ if(enyo.isString(inParams)){
+ //If inParams parameter is a string, use it as request body
+ body = inParams;
+ }
+ else{
+ //If inParams parameter is not a string, build a query from it
+ if(inParams){
+ args.push(enyo.Ajax.objectToQuery(inParams));
+ }
+ }
+ //
if (this.method == "GET") {
if (body) {
args.push(body);
@@ -39,6 +39,7 @@ enyo.AjaxProperties = {
headers: null,
/**
The content for the request body for POST method. If this is not set, params will be used instead.
+ When both params and postBody are set, postBody override any data provided as params thus destroying them.
*/
postBody: "",
/**
@@ -25,9 +25,14 @@ function get() {
function post() {
$q = @$_POST['query'];
if ($q) {
- $result = array('response' => $q);
+ $result = array('response' => "post.".$q);
} else {
- $result = array('response' => file_get_contents('php://input'));
+ $q = @$_GET['query'];
+ if ($q) {
+ $result = array('response' => "query.".$q);
+ }else{
+ $result = array('response' => file_get_contents('php://input'));
+ }
}
$requested_with = @$_SERVER['HTTP_X_REQUESTED_WITH'];
if ($requested_with == 'XMLHttpRequest') {
@@ -41,11 +41,21 @@ enyo.kind({
return inValue == "hello";
});
},
- testPostRequest: function() {
- this._testAjax({url: "php/test2.php", method: "POST"}, {query: "enyo"}, function(inValue) {
- return inValue.response == "enyo";
- });
- },
+ testPostRequestQuery: function() {
+ this._testAjax({url: "php/test2.php", method: "POST"}, {query: "enyo"}, function(inValue) {
+ return inValue.response == "query.enyo";
+ });
+ },
+ testPostRequestQueryWithPayload: function() {
+ this._testAjax({url: "php/test2.php", method: "POST", postBody:"data"}, {query: "enyo"}, function(inValue) {
+ return inValue.response == "query.enyo";
+ });
+ },
+ testPostRequestPayload: function() {
+ this._testAjax({url: "php/test2.php", method: "POST", postBody:"query=enyo"}, null, function(inValue) {
+ return inValue.response == "post.enyo";
+ });
+ },
testPutRequest: function() {
this._testAjax({url: "php/test2.php", method: "PUT"}, null, function(inValue) {
return inValue.status == "put";

0 comments on commit 82bbf22

Please sign in to comment.