继承了我为某个页面评分而复制的代码。这个代码在我的localhost上工作正常但是当我在服务器上传它时它返回语法错误。
function rtgAjax(elm, ratev) {
var cerere_http = get_XmlHttp(); // get XMLHttpRequest object
// define data to be send via POST to PHP (Array with name=value pairs)
var datasend = Array();
for(var i=0; i<elm.length; i++) datasend[i] = 'elm[]='+elm[i];
// joins the array items into a string, separated by '&'
datasend = datasend.join('&')+'&rate='+ratev;
cerere_http.open("POST", 'ratingfiles/ratings.php', true);
// crate the request
cerere_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // header for POST
cerere_http.send(datasend);
// make the ajax request, poassing the data
// checks and receives the response
cerere_http.onreadystatechange = function() {
if (cerere_http.readyState == 4) {
// receives a JSON with one or more item:['totalrate', 'nrrates', renot]
eval(' var jsonitems = ' + cerere_http.responseText);
// if jsonitems is defined variable
if (jsonitems) {
// parse the jsonitems object
for(var rtgitem in jsonitems) {
var renot = jsonitems[rtgitem][2];
// determine if the user can rate or not
// if renot=3 displaies alert that already voted, else, continue with the rating reactualization
if(renot == 3) {
alert("You already voted \n You can rate again tomorrow");
window.location.reload(true); // Reload the page
}
else addRtgData(rtgitem, jsonitems[rtgitem][0], jsonitems[rtgitem][1], renot); // calls function that shows rating
}
}
这一行,
eval(' var jsonitems = ' + cerere_http.responseText);
返回语法错误:意外标识符
答案 0 :(得分:1)
eval
。相反,试试这个:
var jsonitems;
try {
jsonitems = JSON.parse(cerere_http.responseText);
}
catch(e) {alert("Failed to parse JSON: "+e);}