Frage

Ich versuche, einen Ajax-Aufruf mit Jquery zu tätigen. Es scheint zu funktionieren, aber ich schaffe es nicht, das Ergebnis-Div aufzufüllen.

Hier ist der JS-Code:

            <script> 
                $(document).ready(function(){
                    $(document).on('change', '#flip-1', function(){    
                        var datastring = $("#some-form").serialize();
                        $.ajax
                        ({
                            type: "POST",
                            url: "test.php",
                            data:  datastring,
                            dataType: 'html',
                            complete: function(data) {
                                $('#results').html (data);
                                }
                            });
                        });
                    });
            </script>
            <form id="some-form">
                <label for="flip-1">Flip toggle switch:</label>
                <select id="flip-1" name="flip-1" data-role="flipswitch">
                    <option value="off">Off</option>
                    <option value="on">On</option>
                </select>
            </form>

<div id="results">Loading users...</div>

und hier ist der PHP-Code:

<?php

if ($_POST['flip-1'] == 'on') {
    echo 'oh is on!';
} elseif ($_POST['flip-1'] == 'off') {
    echo 'no, its not on';
}
?>      

Das sagt der Chrome-Debugger „Inspect Element“ zu Headern:

Request URL:http://localhost/smart_home/test.php
Request Method:POST
Status Code:200 OK

Request Headers
POST /smart_home/test.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 9
Accept: text/html, */*; q=0.01
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/smart_home/index.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4

Form Data
flip-1=on

Response Headers
HTTP/1.1 200 OK
Date: Thu, 27 Feb 2014 20:58:08 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6
X-Powered-By: PHP/5.5.6
Content-Length: 20
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html

Und das ist, was auf der Registerkarte „Antwort“ steht:Wenn die Taste in die Ein-Position wechselt:

oh is on!      

wenn auf „aus“ geändert

no, its not on      

Hat jemand irgendwelche Ideen?

Danke!

War es hilfreich?

Lösung

Der complete Rückruf empfängt die Antwortdaten nicht direkt (Der erste Parameter ist a jqXHR Objekt).Im Allgemeinen ist es einfacher, das zu verwenden success oder .done() Rückrufe stattdessen:

$.ajax({
    type: "POST",
    url: "test.php",
    data: datastring,
    dataType: 'html',
    success: function (data) {
        $('#results').html(data);
    }
});

Oder

$.ajax({
    type: "POST",
    url: "test.php",
    data: datastring,
    dataType: 'html'
}).done(function (data) {
    $('#results').html(data);
});

Sehen http://api.jquery.com/jQuery.ajax/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top