If you wan't to send pure Json with XMLHttpRequest*, this is how you do it.
You will need to add readyState etc yourself.
* With POST. GET are just there so you can look at something that works. But that's not recommended.
Code:
<html>
<head>
<script type="text/javascript">
var data = {"lat":41.058304899999996,"long":-74.0711244,"email":"[email protected]"};
var xhr = new XMLHttpRequest();
xhr.open('POST', '/json/register', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');
xhr.onload = function () {
// Request finished. Do processing here.
};
xhr.send(JSON.stringify(data));
</script>
<script type="text/javascript">
var data = {"lat":41.058304899999996,"long":-74.0711244,"email":"[email protected]"};
var xhr = new XMLHttpRequest();
xhr.open('GET', '/json/register_get' + '?email=' + JSON.stringify(data), true);
//Send the proper header information along with the request
xhr.setRequestHeader("X-Requested-With",'xmlhttprequest');
xhr.onload = function () {
// Request finished. Do processing here.
};
xhr.send(null);
</script>
</head>
<body>
</body>
</html>
PHP Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Json extends CI_Controller {
public function index()
{
$this->load->view('json');
}
public function register()
{
if( $this->input->is_ajax_request() )
{
$json = $this->input->raw_input_stream;
$json = json_decode($json);
var_dump($json);
}
}
public function register_get()
{
if( $this->input->is_ajax_request() )
{
$json = $this->input->get('email'); // or $_GET['email'];
$json = json_decode($json);
var_dump($json);
}
}
}