How to use ajax () function in jQuery?

In my javascript file, I have the following:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">

$.ajax({

    type: 'post',
    url: 'script.php',
    data: '',
    success: function(output) {
        alert(output[0])
    }

});

</script>

In my PHP file, I have the following:

<?php

echo 'dog';
echo 'cat';

?>

Now I know that I am not passing any data through the ajax function, I just wrote above to check one thing:

When I warn output[0], I get the letter "d". Instead, I want to get a "dog," and when I warn you output[1], I want to get a "cat." How to achieve this?

+3
source share
2 answers

By default, given the answer your call receives, jQuery simply passes you the output as a string, in which case you will need to do any splitting / parsing of the strings if necessary.

. http://api.jquery.com/jQuery.ajax/ , , dataType. , (JSON, XML ..), , $.ajax.

+3

PHP "dogcat". , [0], , , .

PHP script - , "|", javascript .

+1

Source: https://habr.com/ru/post/1787253/


All Articles