简体   繁体   中英

calling php from ajax

I'm trying someting very basic and it doesn't work : (Im getting the alert 'Test' and that's it :( )

<script name="text/javascript">
    function myFunction(part_id, product_id, type)
    {
        alert('test');
        $.ajax({
            type: 'post',
            url: '2.php',
            data: {lname: "www", name: "Natalie"},
            complete: function (txt) {
                alert("complete");
                alert(txt);
            }
        });
    }
</script>

on 2.php I have only one line (on the same directory):

    echo "test has been run";

use with success event.It will get php result.

See the different between success() & complete()

<script name="text/javascript">
    function myFunction(part_id, product_id, type)
    {
        alert('test');
        $.ajax({
            type: 'post',
            url: '2.php',
            data: {lname: "www", name: "Natalie"},
           complete: function (txt) {
               //do something
            },
            success: function (result) {
                alert("success");
                alert(result);//test has been run
            }
        });
    }
</script>

Ajax will return object data:

Object {readyState: 4, responseText: "test has been run", status: 200, statusText: "OK"}

You have to get response text from that object.

function myFunction(part_id, product_id, type)
{
    alert('test');
    $.ajax({
        type: 'post',
        url: '2.php',
        dataType:'text',
        data: {lname: "www", name: "Natalie"},
        complete: function (txt) {
        console.log(txt);
            alert("complete");
            alert(txt.responseText);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM