【问题标题】:jquery success callback function parameter contains enter keyjquery成功回调函数参数包含回车键
【发布时间】:2020-12-19 06:17:47
【问题描述】:

我使用jquery ajax提交了一个php表单,但是当接收到一个回调函数成功的参数值时,该参数值在服务器返回的值前面包含了enter键。

解决方法,我使用包含方法。

有谁知道为什么在服务器返回的值前面插入回车键?

 $.ajax({
    type: "POST",
    url: "_action.php",
    data: dataString,
    success: function(result) {
      n = result.includes("success");
      if (n) {
          grecaptcha.reset();
          $("#spinner-contact").remove();
          $('#success_msg').html("<img class='img-fluid aligncenter mb-3' id='checkmark' src='media/img/form_submitted.jpg' />");  
      }
      else {
          grecaptcha.reset();
          $("#spinner-contact").remove();
          $('#success_msg').html("<div class='mt-3'><strong><h1 class='alert alert-warning'>Contact failed!</strong> Please submit again.</h1></div>");   
      }
    }
  });



$statement = runQPs("INSERT INTO my_guestbook(title, url, path, pos, content, isactive, date) VALUES(
            ?, ?, ?, ?, ?, ?, ?
        )",[$name, $email_from, $address, $telephone, $comments, 0, $now]);

        // Insert contact success or fail 
        if($statement->affected_rows === 0) {
            $result = "fail";
        }
        else {
            $result = "success";
        }

        echo $result;

【问题讨论】:

  • 使用 JSON 格式

标签: php jquery ajax ajaxform


【解决方案1】:

在 PHP 代码中,您可以像这样返回值:

return json_encode(result);

在JS文件中:

success: function(result) {
  // this will be your response from PHP code
  // add you'r IF statement here
  console.log(result);     
}

【讨论】:

  • 是的,看来我得把数据用PHP编码,然后用JS解析。谢谢。
【解决方案2】:

在回显结果变量后,您可以像这样终止语句。

echo $result;die;

die($result);

【讨论】:

  • 响应与我上面第一篇文章中的附图相同。
【解决方案3】:

我认为,你可以在 php 文件中更改你的“echo”以返回“json”

在你的 ajax 代码中:

n = result.includes("success");

var response = result.message(name of return json);

【讨论】:

  • 我已经改成json了,但是还是有回车键。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多