提问者:小点点

尝试使用ajax时javascript中出现意外字符串错误


我正在创建一个ajax方法,使用jquery将数据发送到服务器而无需重新加载,并实现一些用户友好的结果。

但是当我试图将变量传递到url时,我总是收到意外的字符串

我的JavaScript:

jQuery(document).ready(function($){
    $('.popup').on('submit', function(e){
    e.preventDefault(); //prevent a normal postback and allow ajax to run instead

  var user = document.getElementById("search").value
  var text = document.getElementById("text").value
  $.ajax({ 
    data: user,text,
    type: "post", 
    url: "ajaxmessage.php?search="+user"&text="+text, 
    success: function(data) {


    }
   }); 
 });
 });

为什么在&text=“+text附近出现此错误


共1个答案

匿名用户

这是因为您缺少一个+

jQuery(document).ready(function($){
    $('.popup').on('submit', function(e){
    e.preventDefault(); //prevent a normal postback and allow ajax to run instead

  var user = document.getElementById("search").value
  var text = document.getElementById("text").value
  $.ajax({ 
    data: user,text,
    type: "post", 
    url: "ajaxmessage.php?search="+user+"&text="+text,
    success: function(data) {


    }
   }); 
 });
 });

如您所见,我们将user“&text替换为user+”&text