Welcome Guest, Not a member yet? Register   Sign In
Controller is firing but I can't get the $_GET
#10

(This post was last modified: 03-25-2018, 06:20 AM by jreklund. Edit Reason: Added GET )

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);
        }
    }

Reply


Messages In This Thread
RE: Controller is firing but I can't get the $_GET - by jreklund - 03-25-2018, 06:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB