【发布时间】:2011-09-15 22:55:08
【问题描述】:
我在很多地方读到过,将&callback=? 添加到 $.getJSON 中的 URL 将允许跨域 JSON 获取。 (例如参见:http://www.ibm.com/developerworks/library/wa-aj-jsonp1/)
但这对我不起作用。
这是我的表格:
<form id="commentForm_pub4101" class="commentForm" action="/SubmitComment" method="POST">
<h3>Your comment:</h3>
<div class="commentFormTopData commentFormRow">
<label for="Commenter">your name</label>
<input class="commentFormCommenter" id="Commenter" name="Commenter" type="text" value="" />
<label for="Email">e-mail</label>
<input class="commentFormEmail" id="Email" name="Email" type="text" value="" />
</div>
<div class="commentFormBody commentFormRow">
<label for="Body">comment</label>
<textarea class="commentFormBody" cols="20" id="Body" name="Body" rows="2"></textarea>
</div>
</form>
这是我的 jQuery:
$('form[action$="SubmitComment"]').submit(function (event) {
event.preventDefault();
$.getJSON('http://localhost/Comments/index.asp?Title=TESTTitle&Commenter=TESTCommenter&Email=TESTemail&Body=TESTBody&PublicationId=TESTPublicationId&callback=?',
function (data2) {
alert(data2.Title);
}
);
});
目标 URL 是一个经典的 ASP 脚本(不要问为什么——它很复杂),使用 JSON_2.0.4.asp 库返回一个 JSON 结果。我认为这与问题无关,因为这是直接浏览器调用 URL 的返回结果:
{"Commenter":"TESTCommenter","Email":"TESTemail","Body":"TESTBody","PublicationId":"TESTPublicationId","Title":"TESTTitle"}
但如果你坚持,这里是代码(此时是测试):
<!--#include file="JSON_2.0.4.asp"-->
<%
Dim body: body = Request("")
Dim jsa: Set jsa = jsObject()
jsa("Commenter") = Request("Commenter")
jsa("Email") = Request("Email")
jsa("Body") = Request("Body")
jsa("PublicationId") = Request("PublicationId")
jsa("Title") = Request("Title")
jsa.Flush
%>
...所以当我把所有这些测试代码放在一起时....不爱了。没有包含“TESTTitle”的警报窗口。
我做错了什么??
【问题讨论】: