I'm trying to learn how to use Ajax in Codeigniter . This is why I tried to make a small view and a test controller but does not seem to work. Can you help me? This is my view:
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<form name="modulo">
<p>Nome</p>
<p><input type="text" name="nome" id="nome"=></p>
<p>Cognome</p>
<p><input type="text" name="cognome" id="cognome"></p>
<input type="button" id="bottone" value="Invia i dati">
</form>
<div id="risultato"></div>
<script type="text/javascript">
$(document).ready(function() {
$("#bottone").click(function(){
var nome = $("#nome").val();
var cognome = $("#cognome").val();
$.ajax({
type: "POST",
url: "http://lifedesktop/welcome/ajax",
data: "nome=" + nome + "&cognome=" + cognome,
crossOrigin: true,
dataType: "html",
success: function(msg)
{
$("#risultato").html(msg);
},
error: function()
{
alert("Don't work...");
}
});
});
});
</script>
And this is my controller:
public function ajax()
{
if($this->input->is_ajax_request()){
$nome = $_POST["nome"];
$cognome = $_POST["cognome"];
if ($nome == "" || $cognome == "")
{
echo "Inserire nome e cognome!";
}
else
{
echo $nome . " " . $cognome;
}
}
}
In the console web i have this error:
header CORS “Access-Control-Allow-Origin” missing.
Can you help me?