doutui2016 2012-01-20 00:16
浏览 57
已采纳

Jquery .post()远程提交

So Im trying to use jquery's .post function to submit a form and return the results on the same page. I've tried using the example found in jquery's documentation. The form is getting submitted successfully but nothing gets returned to the form page.

I have my JS set up like:

    <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>

the relevant portion of the form script on test3.php looks like:

<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 ('&amp;cb=' + m3_r);



//you Should pass your Keyword

   document.write ('&amp;keyword=<?php echo urlencode($_POST["keyword"]);?>');

//you Should pass your Zipcode

 document.write ('&amp;zipcode=<?php echo urlencode($_POST["zip"]);?>');



   if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);

   document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));

   document.write ("&amp;loc=" + escape(window.location));

   if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));

   if (document.context) document.write ("&context=" + escape(document.context));

   if (document.mmm_fo) document.write ("&amp;mmm_fo=1");

   document.write ("'><\/scr"+"ipt>");

//]]>--></script><noscript><a href='http://mydomain.com/openx/www/delivery/ck.php?n=a9d134b1&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://mydomain.com/openx/www/delivery/avw.php?zoneid=4&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a9d134b1' border='0' alt='' /></a></noscript>
  </div>

What I am doing wrong here? Why doesn't it return the results into the #result div? I've watched the headers, so I know that the data is getting passed successfully.

  • 写回答

1条回答 默认 最新

  • drmeu26880 2012-01-20 01:30
    关注

    If anyone is interested, I have discovered that there is no possible solution to this problem, short of implementing a hack like JSON-P. It doesn't work because it violates the same-domain policy. Thanks to anyone who looked at this and attempted to figure out what was going on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制