所以我尝试使用jquery的.post函数提交表单并在同一页面上返回结果。我尝试过使用jquery's documentation中的示例。表单已成功提交,但没有任何内容返回到表单页面。
我的JS设置如下:
<form id="test_form" method="post" action="test3.php">
<input type="radio" name="zip" id="zip" value="10001" />
<input type="radio" name="keyword" id="keyword" value="GPS" />
<input type="submit" />
</form>
<div id="result" ></div>
<script>
/* attach a submit handler to the form */
$("#test_form").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
zip = $form.find( 'input[name="zip"]' ).val(),
keyword = $form.find( 'input[name="keyword"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url, { keyword: keyword, zip: zip },
function( data ) {
var content = $( data ).find( '#form_results' );
$( "#result" ).empty().append( content );
}
);
});
</script>
test3.php上的表单脚本的相关部分如下所示:
<div id="form_results">
<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://mydomain.com/openx/www/delivery/rtl.php':'http://mydomain.com/openx/www/delivery/rtl.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=4");
document.write ('&cb=' + m3_r);
//you Should pass your Keyword
document.write ('&keyword=<?php echo urlencode($_POST["keyword"]);?>');
//you Should pass your Zipcode
document.write ('&zipcode=<?php echo urlencode($_POST["zip"]);?>');
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
document.write ("&loc=" + escape(window.location));
if (document.referrer) document.write ("&referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://mydomain.com/openx/www/delivery/ck.php?n=a9d134b1&cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://mydomain.com/openx/www/delivery/avw.php?zoneid=4&cb=INSERT_RANDOM_NUMBER_HERE&n=a9d134b1' border='0' alt='' /></a></noscript>
</div>
我在这里做错了什么?为什么不将结果返回到#result div?我看过标题,所以我知道数据已成功通过。
答案 0 :(得分:0)
如果有人有兴趣,我发现没有可能的解决方案来解决这个问题,而不是实施像JSON-P这样的黑客攻击。它不起作用,因为它违反了同域策略。感谢任何看过这个并试图弄清楚发生了什么的人。