I need to get the array and instead of just pushing the data into an html div - get back the php variable.

我需要获取数组而不是仅仅将数据推送到html div中 - 获取php变量。

My $.ajax post ----

我的$ .ajax帖子----

  <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>'

My PHP file is -----

我的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>";

?>'

The goal would be to get back the array and then post certain parts of it to different jquery mobile pages.

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

1 个解决方案

#1


0

You can send JSON data back to AJAX request.

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

Change your arrays to JSON like this:

将数组更改为JSON,如下所示:

echo json_encode($yourData);

To read this, you have to setup your script to accept JSON data

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

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

Then you can access the properties as JavaScript Object.

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

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

更多相关文章

  1. jQuery在点击按钮上迭代/循环遍历数据表
  2. 代替jquery $.post 跨域提交数据的N种形式
  3. 如何在Ajax.ActionLink中获取表单中的输入数据?
  4. jquery处理多维数组
  5. jquery向Django后台发送数组
  6. 如何使用javascript/jquery获取文本框数组值
  7. 开发学习Jquery实例---双击表格弹出模态对话框,编辑提交数据
  8. 执行Django数据库值的计算:视图。py或Javascript模板吗?
  9. jquery 如何获取json指定键为指定值的另一个指定键的值,该值还是

随机推荐

  1. XML指南——XML编码
  2. XML卷之实战锦囊(3):动态分页
  3. XML(6)自己写一个xml序列化器
  4. XML卷之实战锦囊(2):动态查询
  5. XML(5)序列化写入xml文件
  6. XML卷之实战锦囊(1):动态排序
  7. XML(4)XDocument和XmlDocument搜索指定的节
  8. dom4j 操作xml文件(全)
  9. XML(3)XDocument与XmlDocument递归读取xml
  10. 第17天 Android(安卓)Touch事件学习 4 获