我需要从jquery ajax post中回调数据,并将数组拆分为单独的输出


I need to pullback data from a jquery ajax post and break the array out to seperate outputs

我需要得到数组,而不是仅仅把数据推入htmldiv -得到php变量。

我的美元。Ajax post ----

  <script type="text/javascript">
    $(function() {
        $("#login").click(function() {
            var theName = $.trim($("#username").val());
            if(theName.length > 0)
            {
                $.ajax({
                  type: "POST",
                  url: "callajaxdemo.php",
                  data: ({name: theName}),
                  cache: false,
                  dataType: "text",
                  success: onSuccess
                });
            }
        });
        $("#resultLog").ajaxError(function(event, request, settings, exception) {
          $("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
        });
        function onSuccess(data)
        {
            $("#resultLog").html("Result: " + data);
            //$.mobile.changePage('stats.html', { transition: 'slideup'}, true, true);
        }
    });
</script>'
我的PHP文件是-----
<?php
 $username = $_POST['username']; 
 $password = $_POST['password'];
$host = 'https://api.qpme.com/api/accounts/me';
$process = curl_init($host);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($process);
$content = json_decode($return);
/*
echo "<pre>";
print_r($content);
echo "</pre>";
*/
print $content->email . "<br>";
print "<h3>" . "Welcome" . ' ' . $content->firstName . ' ' . $content->lastName . '!' . "<h3>";
?>'

目标是返回数组,然后将它的某些部分发布到不同的jquery移动页面。

可以将JSON数据发送回AJAX请求。

将数组更改为JSON:

echo json_encode($yourData);

要阅读此内容,您必须设置脚本以接受JSON数据

        $.ajax({
          type: "POST",
          url: "callajaxdemo.php",
          data: ({name: theName}),
          cache: false,
          dataType: "json", // <--- Here
          success: onSuccess
        });

然后你可以访问属性作为JavaScript对象。

    function onSuccess(data)
    {
        // access using data.item1
        //           or data.item2 how you prepare your array
    }