Why does my Ajax return an HTML code instead of just returning the value? Codeigniter

Asked

Viewed 88 times

0

Good morning Everybody, everything’s fine?

I have been days trying to return a value from my view to my controller using codeigniter. But I don’t want refresh to happen on the page.

So decide to use ajax, but in my query either it comes with the html code of my page or it gives this error:

{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}

My HTML:

<form real="form" id="form" nome="form" method="post" action='<?= base_url("index.php/welcome");?>'>
                  <select class="form-control" name="selectUser" id="seletor">
                     <option  value='<?= $_SESSION["nome"]?>' >Only me</option>
                     <option  value="Trello">Team</option>
                  </select>
                </form>

My controller:

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

class Welcome extends CI_Controller {

	public function index()
	{	
		
		
		
		$nome = $this->input->post('valor');
		var_dump($nome);
	
		$this->load->model('testemodel');

		$verifica= $this->testemodel->verifica($nome);

		$data['nomecliente'] = $verifica['nome_cliente'];
		$data['horatotal'] = $verifica['hora_total'];
		$data['atividade'] = $verifica['nome_atividade'];
		
		
		
	

		$this->load->view('test', $data);
		
		
	}

My Javascript:

$("#seletor").change(function(){
	
	var value = $("#seletor").val();
	
	$.ajax({
		url: 'welcome/index',
		type: 'POST',
		dataType: "json",
		data: {"valor" : value},
		success: function(data){
			console.log(data);
			
		},
		error: function(data){
			console.log(data);
		}
	});
});

I hope you can help me. I will be very grateful!

1 answer

1


Simply because in the controller you are loading the view:

$this->load->view('test', $data);

It would be easier to make the direct json return:

echo json_encode($data);
  • You are giving this error here on the console.log(); {readyState: 4, getResponseHeader: ?, getAllResponseHeaders: ?, setRequestHeader: ?, overrideMimeType: ?, ...}

  • Take a look at the browser console in the network tab if your request is returning correctly

  • You saved brother! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.