I am trying to make an ajax veryfication but i do not know why it doesn't work.
This is my ajax script:
<script type="text/javascript" language = "javascript">
function checkWords () {
var status = document.getElementById("status");
var u = document.getElementById("styled").value;
if(u != "")
{
status.innerHTML = 'Verifying...';
var hr = new XMLHttpRequest();
hr.open("POST","/topicnou",true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function(){
if (hr.readyState == 4 && hr.status == 200)
{status.innerHTML = hr.responseText;}
}
var v = "name="+u;
hr.send(v);
}
}
</script>
This is my php :
<?php
if(isset($_POST['name']) && $_POST['name'] != ""){
echo 'succes';
}
?>
PS: bought scripts are located in TopicNou.blade.php wich is seen if you go to localhost/topicnou.
This is what appears in my console:
POST http://localhost/topicnou 500 (Internal Server Error)
XHR finished loading: POST "http://localhost/topicnou".
Laravel POST-requests needed Cookie 'laravel_session', you can see it in chrome network tab, for example. Add it to your ajax-request, and i think, it will be work)
@Uranii,
and how do i do that ?
Open google chrome for example, open any your laravel page click f12 on keyboard, in opened console bar open network tab and find you opened path in column "Name" http://prntscr.com/8emtds For example, I'm opened path mysite.com/crm/staff/auth/login, in the screenshot you can see it. And then in this Request headers find Cookie Laravel Session. Copy that and you may use it in your ajax requests. But, it's strange, than I use Jquery AJAX - it's works without laravel_session cookie... may be will be better to use JQuery Ajax?
$.post('/path', {name: name, (optional) laravel_session: copied_session}, function(response){
$(body).append(response)
})
it's realy works, even without laravel_session.