我需要从jquery ajax post中撤回数据并将数组分解为单独的输出
16lz
2021-01-22
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
}
更多相关文章
- jQuery在点击按钮上迭代/循环遍历数据表
- 代替jquery $.post 跨域提交数据的N种形式
- 如何在Ajax.ActionLink中获取表单中的输入数据?
- jquery处理多维数组
- jquery向Django后台发送数组
- 如何使用javascript/jquery获取文本框数组值
- 开发学习Jquery实例---双击表格弹出模态对话框,编辑提交数据
- 执行Django数据库值的计算:视图。py或Javascript模板吗?
- jquery 如何获取json指定键为指定值的另一个指定键的值,该值还是