<div class="grid--cell fl1 lh-lg">
<div class="grid--cell fl1 lh-lg">
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, <a href="/help/reopen-questions">visit the help center</a>.
</div>
</div>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2012-09-08 23:57:11Z" class="relativetime">7 years ago</span>.</div>
</div>
</aside>
So in trying to have javascript pass values to a php script using Ajax i get the following error message.
Uncaught SyntaxError: Unexpected end of input
In stepping through the code I found saw that my response from my script came back a null string. Would someone please point me to my mistake, I cannot seem to get a grasp on 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)
{
// function returns "AJAX" object, depending on web browser
// this is not native JS function!
request = getHTTPObject();
request.onreadystatechange = sendData;
request.open("GET", "request.php?json="+JSONstring, true);
request.send(null);
}
// function is executed when var request state changes
function sendData()
{
// if request object received response
if(request.readyState == 4)
{
// parser.php response
var JSONtext = request.responseText;
// convert received string to JavaScript object
var JSONobject = JSON.parse(JSONtext);
// notice how variables are used
var msg = "Number of errors: "+JSONobject.errorsNum+
"
- "+JSONobject.error[0]+
"
- "+JSONobject.error[1];
alert(msg);
}
}
my php file.
<?php
//request.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 invoke ajax.
<html>
<head>
<script src="request.js">
</script>
</head>
<body>
<a href="Javascript:runAjax('vinoth')">call</a><br>
</body>
</html>
Thanks in advance.
</div>