-2

这是我在某处复制的用于对页面进行评分的代码。此代码在我的本地主机上运行良好,但当我将其上传到服务器时它返回语法错误。

 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);

返回语法错误:意外的标识符

4

1 回答 1

1

eval不应该以这种方式使用。相反,试试这个:

var jsonitems;
try {
    jsonitems = JSON.parse(cerere_http.responseText);
}
catch(e) {alert("Failed to parse JSON: "+e);}
于 2013-03-05T02:59:45.007 回答