Permalink
Browse files

fix(ajax): elgg/Ajax view() and form() set $vars as expected

The `elgg/Ajax` module was auto-converting any request with `options.data`
to use the `POST` method. This makes sense for things like actions, which
may send a large amount of form data, but it caused `$vars` to not be
populated as expected because only `GET` params are injected.

If you specified `options.data` with these methods before, note the server
will no longer receive this data as `$_POST` on the server.

Fixes #10667
  • Loading branch information...
mrclay committed Dec 23, 2016
1 parent 11c4640 commit abf8a9ce87117ab24cb62e937805750eca780de1
View
@@ -115,25 +115,34 @@ define(function(require) {
expect(ajax._ajax_options.method).toEqual('POST');
});
$.each(['path', 'form', 'view'], function (i, method) {
it("path(): non-empty object data changes default to POST", function() {
ajax.path('foo', {
data: {bar: 'bar'}
});
expect(ajax._ajax_options.method).toEqual('POST');
});
it(method + "() defaults to GET", function() {
ajax[method]('foo');
expect(ajax._ajax_options.method).toEqual('GET');
it("path(): non-empty string data changes default to POST", function() {
ajax.path('foo', {
data: '?bar=bar'
});
expect(ajax._ajax_options.method).toEqual('POST');
});
it(method + "(): non-empty object data changes default to POST", function() {
$.each(['form', 'view'], function (i, method) {
it(method + "(): non-empty object data left as GET", function() {
ajax[method]('foo', {
data: {bar: 'bar'}
});
expect(ajax._ajax_options.method).toEqual('POST');
expect(ajax._ajax_options.method).toEqual('GET');
});
});
it(method + "(): non-empty string data changes default to POST", function() {
ajax[method]('foo', {
data: '?bar=bar'
});
expect(ajax._ajax_options.method).toEqual('POST');
$.each(['path', 'form', 'view'], function (i, method) {
it(method + "() defaults to GET", function() {
ajax[method]('foo');
expect(ajax._ajax_options.method).toEqual('GET');
});
it(method + "(): empty string data leaves default as GET", function() {
View
@@ -18,6 +18,7 @@ function developers_init() {
elgg_register_page_handler('developers_ajax_demo', 'developers_ajax_demo_controller');
elgg_register_external_view('developers/ajax'); // for lightbox in sandbox
elgg_register_ajax_view('developers/ajax_demo.html');
$sandbox_css = elgg_get_simplecache_url('theme_sandbox.css');
elgg_register_css('dev.theme_sandbox', $sandbox_css);

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,7 @@
<?php
if (!isset($vars['entity']) || !$vars['entity'] instanceof ElggSite) {
register_error('$vars not set by ajax.form()');
}
?><p>view demo</p>
@@ -52,14 +52,18 @@ define(function(require) {
if (html_page.indexOf('path demo') != -1) {
log("PASS path()");
return ajax.view('developers/ajax_demo.html');
return ajax.view('developers/ajax_demo.html', {
data: {guid: 1}
});
}
})
.then(function (div) {
if (div.indexOf('view demo') != -1) {
log("PASS view()");
return ajax.form('developers/ajax_demo');
return ajax.form('developers/ajax_demo', {
data: {guid: 1}
});
}
})
.then(function (form) {
@@ -1,4 +1,8 @@
<?php
// This will be fetched via ajax by the developers/ajax_example AMD module
if (!isset($vars['entity']) || !$vars['entity'] instanceof ElggSite) {
register_error('$vars not set by ajax.view()');
}
echo "form demo";
@@ -246,6 +246,7 @@ define(function (require) {
options = options || {};
options.url = 'ajax/view/' + view;
options.method = options.method || 'GET';
// remove query
view = view.replace(query_pattern, '').replace(slashes_pattern, '');
@@ -274,6 +275,7 @@ define(function (require) {
options = options || {};
options.url = 'ajax/form/' + action;
options.method = options.method || 'GET';
// remove query
action = action.replace(query_pattern, '').replace(slashes_pattern, '');

0 comments on commit abf8a9c

Please sign in to comment.