ajax jquery settings
So say I want to import (using ajax) a script which contains both some raw html and a java script:
<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>
is there anything wrong with the following function?
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
and ln
are supposed to be parameters in the url. This works when i use the $.get
method, but does it work the same for $.ajax??
I set datatype to "script" so that the javascript would be recognized. But is this correct? I'm 开发者_如何转开发not sure I know what I'm doing yet.
Thanks for your help
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:
- You may want to explicitly specify it's a
GET
request usingtype: 'GET'
. - 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) .html
will indeed insert thescript
tag, but it won't execute it. To get it to execute, you must remove and then re-insert thescript
tag.
精彩评论