【问题标题】:How used consoled log to ajax param?如何使用控制台日志到 ajax 参数?
【发布时间】:2023-03-28 19:27:01
【问题描述】:

我正在尝试使用此控制台日志

$.post("greeting", { salutation: console.log(5 + 6), name: "Friend" },

到 ajax 参数称呼但不起作用

我该如何解决这个问题

<html>
  <head>
    <script src="javascripts/jquery-1.5.2.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $.post("greeting", { salutation: console.log(5 + 6), name: "Friend" },
          function(result) { $("#greeting").html(result);
        });
      });
    </script>
  </head>
  <body>
    <h1>Sinatra Ajax Demo using jQuery</h1>
    <div id="greeting">
      <p>Hello, World</p>
    </div>
  </body>
</html>

请帮帮我

我也使用 sinatra ruby​​ 并且不打印 11

require 'sinatra'
require 'pp'

get '/' do
  erb :index
end

post '/greeting' do
  #puts "params"
  #pp params

  "<p>#{params[:salutation]}, #{params[:name]}</p>"
end

【问题讨论】:

  • 您的代码对我来说工作得很好。看看jsfiddle.net/q3ebh98p
  • 嗨,我用 sinatra ruby​​ 测试过,但没有从控制台 5+6 打印 11
  • salutation: console.log(5 + 6) 导致 a) 11 登录到控制台,b) 称呼设置为 undefined ...这是您所期望的吗?
  • 不是我预期的 11 但我不知道做错了什么
  • 我不确定

标签: javascript jquery ajax sinatra


【解决方案1】:

试试这个方法:

<html>
  <head>
    <script src="javascripts/jquery-1.5.2.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        var param = { salutation: 5+6, name: "Friend" };
        $.post("greeting", param, function(result) { 
          console.log("param",param);
          $("#greeting").html(result);
        });
      });
    </script>
  </head>
  <body>
    <h1>Sinatra Ajax Demo using jQuery</h1>
    <div id="greeting">
      <p>Hello, World</p>
    </div>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多