如何从php使用json在phonegap检索数据? [英] How to retrieve data from php using json in phonegap?

查看:71
本文介绍了如何从php使用json在phonegap检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例php代码:

 <?php 
$ nai = 18;
print json_encode($ nai);
?>

我在中添加了这些行www.example.com/reply.php



我想在jquery中获取这个值。

 

function connect(){
alert(chek);
var term =nai;
$ .ajax({
url:'http://www.example.com/reply.php',
type:'POST',
data:term,
dataType:'json',
错误:function(jqXHR,text_status,strError){
alert(no connection);
},
timeout:60000,
success:function(data){
alert(data);
}
});
}


解决方案


$ b

默认情况下,html会被渲染,在代码中使用这个来解决这个问题。

 <?php 
header('Content-Type:application / json');
$ nai = 18;
print json_encode($ nai);
?>

现在这是一个可读的json格式 phonegap


This is my sample php code :

<?php
    $nai = 18;
    print json_encode($nai);
?>

I added these lines in www.example.com/reply.php.

I want to get this value in jquery. so that i can use the value to process something.

Is the code right??

function connect() {
    alert("chek");
    var term = "nai";
    $.ajax({
        url: 'http://www.example.com/reply.php',
        type: 'POST',
        data: term,
        dataType: 'json',
        error: function(jqXHR, text_status, strError){
            alert("no connection");
        },
        timeout: 60000,
        success: function(data) {
            alert(data);
        }
    });
}

解决方案

You need to set the header of the file that PHP is rendering.

By default html is rendered, use this in your code to fix this problem.

<?php
    header('Content-Type: application/json');
    $nai = 18;
    print json_encode($nai);
?>

Now this is a readable json format for phonegap.

这篇关于如何从php使用json在phonegap检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆