$.getJSON just not working for me
I've read in numerous places that adding &callback=?
to the URL in $.getJSON will allow cross-domain JSON fetching. (e.g. see: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/)
But it's just not working for me.
Here's my form:
<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="co开发者_开发百科mmentFormBody commentFormRow">
<label for="Body">comment</label>
<textarea class="commentFormBody" cols="20" id="Body" name="Body" rows="2"></textarea>
</div>
</form>
Here's my 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);
}
);
});
The target URL is a classic-ASP script (don't ask why -- it's complicated), using the JSON_2.0.4.asp library to return a JSON result. I don't think that's related to the problem, because here's the returned result of a direct browser-call to the URL:
{"Commenter":"TESTCommenter","Email":"TESTemail","Body":"TESTBody","PublicationId":"TESTPublicationId","Title":"TESTTitle"}
But if you insist, here's the code (it's test at this point):
<!--#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
%>
...So when I put all this test code together .... no love. No alert window with 'TESTTitle' in it.
What am I doing wrong??
To do cross-donain JSON, it uses the JSONP format.
When a JSON response looks like this:
{"name":"value"}
the corresponding JSONP response looks like this:
callBackName({"name":"value"})
Where the callBackName
is the callback that you send in the query string.
So, your ASP code has to pick up that parameter, and wrap the JSON in a function call.
JSONP needs a callback wrapper. See http://en.wikipedia.org/wiki/JSONP
e.g. example.com?foo.php?callback=bar
returns
bar({json});
callback=?
will be replaced with something like callback=jQuery1238123883
. You ASP page is responsible to get that callback value. Then wrap the return JSON in a function with that callbacks name if and only if callback is set.
I've seen a similar problem caused by jQuery escaping the ?
before realising that this was supposed to be a JSONP call.
The solution was to use an alternate parameter set:
$.getJSON('http://localhost/Comments/index.asp?json.wrf=?', {
Title: 'TESTTitle',
Commenter: 'TESTCommenter',
Email: 'TESTemail',
Body: 'TESTBody',
PublicationId: 'TESTPublicationId'
});
Try this:
$.getJSON('http://localhost/Comments/index.asp?Title=TESTTitle&Commenter=TESTCommenter&Email=TESTemail&Body=TESTBody&PublicationId=TESTPublicationId&callback=?',null,
function (data2) {
alert(data2.Title);
}
);
精彩评论