如何从纯JS Ajax转换为jQuery Ajax?

我正在研究这个:

if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
   }
   xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         document.getElementById("txtuser").innerHTML = xmlhttp.responseText;
      }
   }
   xmlhttp.open("GET", "ajax.php?operation=get" + "&q=" + str, true);
   xmlhttp.send();

发送到 jquery...

我已有的代码:

$.ajax({
            type: "GET",
            url: "ajax.php",
            data: "operation=get" + "&q="+ str,
            success: function(){
                console.log('done');
            }
        });
    return false;

我做错了什么?

Maybe you are passing your data in the wrong way.. try with

data: { operation: 'get', q: str }

I think your issue is with your data. jQuery is expecting the data to be JSON, so what you need is more like this:

$.ajax({
        type: "GET",
        url: "ajax.php",
        data: { operation: 'get', q: str},
        success: function(){
            console.log('done');
        }
 });
return false;

that should do what you want, jQuery will encode it into the URL for you automatically when the request is made.