总是有同样的问题,查询移动访问动态数据。我使用jquery和jquerymobile
我可以看到firebug如何JSON是OK的,但我尝试恢复的数据可以在列表视图中使用,不能捕获它。
这是我的代码:http://jsfiddle.net/lordnigh/LD6YA/9/如果我定义函数错误总是显示,但我不知道为什么,JSON的sintaxis是正确的。
JSON的输出如下:
$(document).on('pageinit', '#home', function(){
var url2 = 'http://www.domain.com',
mode2 = 'ws.php?TIPO=',
key2='OK';
function logicaCliente(data){
console.log(data);
}
$.ajax({
url: url2+mode2+key2 ,
dataType: "jsonp",
async: true,
success: function (result) {
alert(result);
$.each(result, function(i, row) {
console.log(JSON.stringify(row));
$('#data-list').append('<li><a href="" data-id="' + row.id + '"><h3>' + row.nombre + '</h3><p>' + row.denominacion + '</p></a></li>');
});
$('#data-list').listview('refresh');
},
error: alert('Error en respuesta'),
jsonpCallback: 'logicaCliente'
});
});
}
谁能帮我一下我放错了什么地方?[{"id":"1","数量":"功能"、"precio":"25"、"denominacion":"道格:拉里奥哈省"},{" id_vino":"2","数量":"马丁内斯单面山"、"precio":"20"、"denominacion":"道格:拉里奥哈省"})
的问候罗伯特。
放error:function (a,b,c){ console.log(a,b,c); alert('Error en respuesta')},
会显示错误:
Object {readyState: 4, getResponseHeader: function,gettallresponseheaders:函数,setRequestHeader:函数,"parsererror"错误{堆栈:(…),message: "logicaCliente was not call "}
logicaCliente was not called
:这是来自jQuery。
回调需要执行,但它必须是application/javascript
类型,而不是
look:
HTTP/1.1 200 OK
Date: Sun, 16 Mar 2014 12:50:56 GMT
Server: Apache
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json <-----------
你的反应不是jsonp
而是json
: "Content-Type:application/json"
同样,CORS头文件不存在。所以我看不出这是怎么回事。(在ie7中,它会(通过<script>
,因为数组的原型破解-但这是另一个无关的故事)
你确定你正在调用正确的API吗?
<标题>编辑当然不行。我从jsfiddle.com
运行它-你的小提琴是无用的。(对我们的测试:-)
请添加error:function (a,b,c){ alert(a+' '+b+' '+c);
并告诉我们结果是什么。
<标题>也:移动function logicaCliente(data){
console.log(data);
}
到全局范围>不在函数内部
标题>标题>我能找到解决办法问题是Webservice,如果你使用jsonp: 'jsoncallback'需要在WS返回$_GET['jsoncallback']如何JSON对象,如果没有放它,不要找到任何对象和做错误。
例如:echo $_GET['jsoncallback']。"("。美元json_encode(记录)。"),";
我已经在两个答案之间做了一个修复,我可以在console中看到show被执行jsonpCallback,但在做错误并且不填充列表视图之后:
错误:http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js正在被分配一个//# sourceMappingURL,但已经有一个回调successundefined Cadena vacía pasada a getElementById().
(c) {var……d = this._search; this._timer&及(b.clearTimeout (this._timer) this._timer…
jquery....min.js (línea 8) parsererror;错误:未定义不是[object object] pagina.html (línea 52)
代码修正为:
$(document).on('pageinit', '#home', function(){
var stuff = {
id:null,
nombre:null,
precio:null,
denominacion:null
};
var jsonString = JSON.stringify(stuff);
$.ajax({
url: "http://www.dominio.com/ws.php?TIPO=OK" ,
crossDomain: true,
type:"GET",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
async: true,
data: jsonString,
success: function (result) {
alert(result);
$.each(result, function(i, row) {
console.log(JSON.stringify(row));
$('#data-list').append('<li><a href="" data-id="' + row.id + '"><h3>' + row.nombre + '</h3><p>' + row.denominacion + '</p></a></li>');
});
$('#data-list').listview('refresh');
},
error: function(xhr, status, error){
console.log(status + '; ' + error+ ';');},
jsonpCallback:function(response) {
console.log('callback success'+response);
}
});
});
在webserver是非常简单的,只做一个选择和封装在JSON中,我尝试与内容类型:应用程序/javascript"one_answers"内容类型:应用程序/JSON ",但同样的结果,这是web服务代码
<?php
include($DIRCONF . 'conf/VARIABLES.ini.php'); //incluimos configuración
include($DIRCONF . 'JSON.php');
$tipo=$_GET['TIPO'];
$json = new Services_JSON;
$conexion = mysql_connect(SERVIDOR_MYSQL, USUARIO_MYSQL, PASSWORD_MYSQL);
mysql_select_db(BASE_DATOS, $conexion);
$que = "SELECT * FROM `DATOS`";
$res = mysql_query($que, $conexion) or die(mysql_error());
while ($row = mysql_fetch_assoc($res)) {
$data[] = $row;
}
//Cerramos la conexion a la base de datos
mysql_close($conexion);
//header("Content-type: application/javascript");
header("Content-type: application/json");
echo json_encode($data) ;
?>
试试下面的代码:
$(document).on('pageinit', '#home', function(){
var url2 = 'http://www.domain.com/',
mode2 = 'ws.php?TIPO=',
key2='OK';
function logicaCliente(data){
console.log(data);
}
var stuff = {"id":"2","nombre":"Martinez la cuesta","precio":"20","denominacion":"DOC: La Rioja"};
var jsonString = JSON.stringify(stuff);
$.ajax({
url: url2+mode2+key2 ,
dataType: "jsonp",
async: true,
data: jsonString,
success: function (result) {
alert(result);
$.each(result, function(i, row) {
console.log(JSON.stringify(row));
$('#data-list').append('<li><a href="" data-id="' + row.id + '"><h3>' + row.nombre + '</h3><p>' + row.denominacion + '</p></a></li>');
});
$('#data-list').listview('refresh');
},
error: alert('Error en respuesta'),
jsonpCallback: 'logicaCliente'
});
});
}