假设我想导入一个脚本(使用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。但这是对的吗?我不知道我还应该做什么......
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:
GET
request using type: 'GET'
.script
is the wrong data type. (script
will try to evaluate the HTML as JavaScript, which won't work).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.