Welcome Guest, Not a member yet? Register   Sign In
Help in converting vanilla PHP to CI (AJAX + filter + infinity scroll)
#6

(This post was last modified: 03-23-2018, 08:23 AM by jreklund.)

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Json extends CI_Controller {
    
    public function 
return_json()
    {
        if( !
$this->input->is_ajax_request() )
        {
            exit;
        }
        
        
$json $this->input->raw_input_stream;
        
$json json_decode($json);
        
/**
        $json now looks like
            stdClass Object
            (
                [group_no] => 1
                [brand] => brand
                [material] => material
                [size] => size
            )
            
        Access it like
        echo $json->group_no;
        **/
        
        // Process json...
        
        // Data to send back
        
$data = array('success' => 'true');
        
        
// Send back json
        
$this->output
                
->set_content_type('application/json')
                ->
set_output(json_encode($data));
    }
    
    public function 
return_html()
    {
        if( !
$this->input->is_ajax_request() )
        {
            exit;
        }
        
        
$json $this->input->raw_input_stream;
        
$json json_decode($json);
        
// Process json...
        
        // Data to send back
        
echo '<article class="col-md-4 col-sm-6">...</article>';
    }

Code:
$.ajax({
    url : "/json/return_json/",
    type: "POST",
    data: JSON.stringify(
        {group_no: 1, brand: 'brand', material: 'material', size: 'size'}
    ),
    contentType: "application/json; charset=utf-8",
    dataType   : "json",
    success    : function( returnedJson ){
        console.log("Pure jQuery Pure JS object");
        console.log( returnedJson );
    }
});

$.ajax({
    url : "/json/return_html/",
    type: "POST",
    data: JSON.stringify(
        {group_no: 1, brand: 'brand', material: 'material', size: 'size'}
    ),
    contentType: "application/json; charset=utf-8",
    dataType   : "html",
    success    : function( returnedHtml ){
        console.log("Normal html");
        console.log( returnedHtml );
    }
});
Reply


Messages In This Thread
RE: Help in converting vanilla PHP to CI (AJAX + filter + infinity scroll) - by jreklund - 03-23-2018, 08:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB