weixin_33725239 2011-09-29 01:53 采纳率: 0%
浏览 37

jQuery设置函数有什么问题吗?

假设我想导入一个脚本(使用Ajax),该脚本包含一些原始的html和一个java脚本:

<p>To comment, please first prove that you are human being</p>
<form method='post' action='../recapatcha_verify.php'>
<script type="text/javascript"
 src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
 </script>
<input type='submit'/> 
</form>

下面的函数有什么问题吗?

  function comments(file, id, fs, pn, ln)
   {
      $("div#commentWrapper").show(function(){
       $.ajax({
           url: "../commentfiles/" + file,
           pid: id,
           fs: fs,
           pn: pn,
           ln: ln,
           dataType: script,
           success: function(txt)
                 {
            $("div#commentWindow").html(txt);
                  }
       });
      });

      }

PID、fs、pn和ln应该是url中的参数。当我使用$.get方法时,这是可行的,但是它对$.ajax是否同样有效呢?

我将数据类型设置为“script”,以便能够识别javascript。但这是对的吗?我不知道我还应该做什么......

  • 写回答

1条回答 默认 最新

  • weixin_33676492 2011-09-29 02:10
    关注

    To answer one of your questions, no, $.ajax does not take parameters the same way as $.get. To send parameters, you must use data:

    $.ajax({
        ...
        data: {param1: 1, param2: 2, param3: 3},
        ...
    });
    

    Secondly, right now you're setting dataType to the value of a variable named script. You must set it to the string script:

    $.ajax({
        ...
        dataType: 'script',
        ...
    });
    

    Some other things:

    1. You may want to explicitly specify it's a GET request using type: 'GET'.
    2. If it's HTML that contains a script, script is the wrong data type. (script will try to evaluate the HTML as JavaScript, which won't work)
    3. .html will indeed insert the script tag, but it won't execute it. To get it to execute, you must remove and then re-insert the script tag.
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog