Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created September 1, 2011 16:40
Show Gist options
  • Select an option

  • Save vaclavbohac/1186609 to your computer and use it in GitHub Desktop.

Select an option

Save vaclavbohac/1186609 to your computer and use it in GitHub Desktop.
Small library enabling simple XHR requests to the Nette Framework powered backends.
<?php
require __DIR__ . '/libs/Nette/loader.php';
Nette\Diagnostics\Debugger::enable();
$configurator = new Nette\Configurator();
$context = $configurator->getContainer();
$payload = array('foo' => 'bar');
$response = new Nette\Application\Responses\JSONResponse($payload);
$response->send($context->httpRequest, $context->httpResponse);
// Copyright 2011 Vaclav Bohac <[email protected]>. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Small library enabling simple XHR requests
* to the Nette Framework powered backends.
*/
goog.require('nclosure.xhr');
var testAjaxRequest = function () {
asyncTestCase.waitForAsync();
nclosure.xhr.get('ajax.php', function (response) {
assertObjectEquals('Should receive foo : bar object from ajax.php', {'foo':'bar'}, response);
asyncTestCase.continueTesting();
});
};
var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
// Copyright 2011 Vaclav Bohac <[email protected]>. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Small library enabling simple XHR requests
* to the Nette Framework powered backends.
*/
goog.provide('nclosure.xhr');
goog.require('goog.events')
goog.require('goog.net.XhrIo');
/**
* This additional header helps Nette Framework
* and maybe other frameworks aswell identify that
* HTTP request was sended via AJAX.
* @typedef {{string: string}}
*/
nclosure.xhr.XHR_HEADER = {
'X-Requested-With': 'XMLHttpRequest'
};
/**
* Makes AJAX GET request to the uri. On success
* calls callback function with JSON payload.
* @param {!string} uri uri to be requested
* @param {function(object)} success function called on success
*/
nclosure.xhr.get = function (uri, success) {
goog.net.XhrIo.send(uri, function (e) {
if (typeof success === 'function') {
success(e.target.getResponseJson());
}
}, null, 'GET', nclosure.xhr.XHR_HEADER);
};
goog.exportSymbol("nclosure.xhr.get", nclosure.xhr.get);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment