代码之家  ›  专栏  ›  技术社区  ›  ayush

从远程PHP脚本返回数据

  •  0
  • ayush  · 技术社区  · 14 年前

    我正在使用php/ajax提交一个没有页面刷新的表单。这是我的档案-

    Coupon JS

    jQuery(document).ready(function(){
            jQuery(".appnitro").submit( function(e) {
    $.ajax({
                url     : "sms.php",
                type    : "post",
                dataType: "json",
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }
    
            });
    //return false or
    e.preventDefault();
    
        });
    
    });
    

    SMS.PHP

        <?php
        //process form
    $res = "Message successfully delivered";
        $arr = array( 'mess' => $res );
        echo json_encode( $arr );//end sms processing
        unset ($_POST);
        ?>
    

    这是我的HTML页面代码-

    <form id="smsform" class="appnitro" action="sms.php" method="post">
    ...
    </form>
    <div id="mess" style="background:green;"></div>
    

    现在不是通过Ajax提交表单而不刷新页面,而是将页面重定向到

    baseurl/sms.php 页面上唯一可见的是

    {"mess":"Message successfully delivered"}
    

    我猜PHP脚本没有成功返回jquery,因此会显示sms.php最后一部分中的echo。 如何使PHP脚本成功返回? 关于如何调试这个的任何想法。我尝试使用 return false 在coupon.js的末尾,但没有结果。

    当我点击提交时,Firebug给出了以下结果-

    POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
    
    404 Not Found 1.29s   `jquery.min.js (line 130)`
    

    响应

    Firebug needs to POST to the server to get this information for url:
    http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
    
    This second POST can interfere with some sites. If you want to send the POST again, open a new tab in Firefox, use URL 'about:config', set boolean value 'extensions.firebug.allowDoublePost' to true
    This value is reset every time you restart Firefox This problem will disappear when https://bugzilla.mozilla.org/show_bug.cgi?id=430155 is shipped
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Michał Kluczka    14 年前

    <form id="..." class="..." ... onsubmit="return false">
    

    // instead of  
    url: "sms.php"
    // try
    url: "/~kunal17/devbuzzr/wp-content/themes/street/sms.php"
    // although I don't really know if it will help
    
        2
  •  1
  •   Fanis Hatzidakis    14 年前

    sms.php

    $(document).ready(function(){
            $(".appnitro").submit( function(e) {
    $.ajax({
    ...
    

        3
  •  1
  •   Community CDub    8 年前

    Fanis Michal Kluczka

    alert('X') jQuery(document).ready() jQuery(".appnitro").submit()

    header('Content-Type: application/json');
    

    sms.php Don’t serve JSON as text/html