Therefore, trying to pass javascript values to php script using Ajax, I get the following error message.
Uncaught SyntaxError: Unexpected end of input
While passing through the code that I found, I saw that my answer from my script returned a null string. Someone, please, point me to my mistake, I cannot understand it.
Here is my javascript (request.js)
var request;
function getHTTPObject()
{
var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try
{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xhr = false;
}
}
}
return xhr;
}
function runAjax(JSONstring)
{
request = getHTTPObject();
request.onreadystatechange = sendData;
request.open("GET", "request.php?json="+JSONstring, true);
request.send(null);
}
function sendData()
{
if(request.readyState == 4)
{
var JSONtext = request.responseText;
var JSONobject = JSON.parse(JSONtext);
var msg = "Number of errors: "+JSONobject.errorsNum+
"\n- "+JSONobject.error[0]+
"\n- "+JSONobject.error[1];
alert(msg);
}
}
my php file.
<?php
$decoded = json_decode($_GET['json']);
$json = array(
'errorsNum' => 2,
'error' => array(
"error 1","error 2!"
)
);
$encoded = json_encode($json);
die($encoded);
?>
and finally my html file where I call ajax.
<html>
<head>
<script src="request.js">
</script>
</head>
<body>
<a href="Javascript:runAjax('vinoth')">call</a><br>
</body>
</html>
Thanks in advance.
source
share