weixin_33714884 2014-05-14 06:45 采纳率: 0%
浏览 54

ajax意外令牌*

I have ajax script but it returns unexpected token *. I use ajax example from w3school.com and it different from usually. This is my script:

<script>  
$("#conform").click(function ()
{
   var a= $(this).closest(".col").prev().find("#prod_qty").val();
   var b= $(this).prev().val();
   var c= $(this).closest("#highlight_product").find("#name").val();
   var d= b * c;
   var e= "<?php echo $_SESSION['name'] ?>";
   var f= "<?php echo $_SESSION['email'] ?>";
}
)

    function buy() 
    {   
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
     {
     alert ('Browser does not support HTTP Request');
     return;
     }
    var url='buy.php?user='+e+'&email='+f+'&product='+c+'&count='+a+'&total='+d;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open('GET',url,true);
    xmlHttp.send(null);*/
    }

    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')
     { 
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
     }
    }

    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
     }
    return xmlHttp;

}

I try find it in google, It says problem with dataType, It must HTML. But i dont know how to change it, anyone know?

  • 写回答

2条回答 默认 最新

  • weixin_33743248 2014-05-14 06:47
    关注

    Can I suggest using Jquery AJAX wrapper? It's cross browser compliant, much neater and lets gives you a handy callback function on completion(really important) since the result is ASYNC which allows you to retrieve data before execution.

    $.ajax({
        type: "POST",
        url: '',
        data : '',
        dataType: 'JSON',
        }).done(function(data) {
     // this will be run when the AJAX request completes
        var ParsedData= jQuery.parseJSON(data);
        // this will be run when the AJAX request succeeds
    }).fail(function() {
        // this will be run when the AJAX request fails
    }).always(function() {
        // this will be run when the AJAX request is complete, whether it fails or succeeds
    });
    
    }
    
    评论

报告相同问题?