Javascript Json结果添加到html容器
我有一个通过Web服务得到的Json结果,现在我想看看它是否有效,如何使Json值显示在按钮或链接中?还有页面加载 返回的值之一如下所示:“name”:“Muhammad Ali”, “昵称”:“最伟大的” json和javcascript非常新 Javascript和json:Javascript Json结果添加到html容器,javascript,json,html,cordova,Javascript,Json,Html,Cordova,我有一个通过Web服务得到的Json结果,现在我想看看它是否有效,如何使Json值显示在按钮或链接中?还有页面加载 返回的值之一如下所示:“name”:“Muhammad Ali”, “昵称”:“最伟大的” json和javcascript非常新 Javascript和json: function Getdata() { $.ajax({ type: "POST", data: "{}", url: "https://raw.git
function Getdata() {
$.ajax({
type: "POST",
data: "{}",
url: "https://raw.github.com/appcelerator/Documentation-Examples/master/HTTPClient/data/json.txt",
contentType: "application/json; cherset=utf-8",
datatype: "json",
success: loadpage,
failure: givealert
});
function loadpage(result) {
if (resu.hasOwnProperty("d")) { result = res.d; }
var data = JQuery.parseJSON(result);
}
function givealert(error) {
alert('failed!!' + error);
}
}
现在,如何让它在标签和表单加载中显示来自web服务的一个值?
标签/按钮的html标记:
<div id="listheight">
<a type="button" id="routing" href="#datatapage"></a>
</div>
我正在使用cordova/phonegap、Visual studi2010、html、javascript、css、jquerymobile和jquery1.8.2
提前谢谢 首先,使用此HTML
<a type="button" id="routing" href="#datatapage" onclick="Getdata()">Click</a>
并将代码更改为
jQuery.support.cors = true;
function Getdata() {
var request = $.ajax({
type: "POST",
data: "{}",
dataType: 'json',
url: "data/json.txt", // better use a relative url here
mimeType: "application/json; cherset=utf-8",
success: loadpage,
failure: givealert
});
function loadpage(result) {
// this only displays you the values in a messagebox for you to check if it works
// you can remove the following two lines
alert("Name = "+result.fighters[0].name);
alert("Nickname = "+result.fighters[0].nickname);
// this changes the text
document.getElementById("routing").innerHTML = result.fighters[0].name;
}
function givealert(error) {
alert('failed!!' + error);
}
}
我为此创建了一个JSFIDLE:
请注意,我发送请求的URL是由JSFIDLE提供的,指向我复制您的示例数据的地方。您在您的var数据中得到了什么吗……我的意思是var data=JQuery.parseJSON(result)。。如果是。。post wat u r json(返回值)(您案例中的数据)…有什么方法可以检查吗?因为windows phone sdk不支持javascript调试?谢谢你的快速回复:)\hey Lukas,谢谢你的快速回复我照你说的做了,但申请仍然没有回复。我甚至还去玩了一次?甚至没有点击我在函数getdata()后发出的警报?在我的评论上编辑-它实际上点击了函数,只是没有将任何数据加载到该按钮中。您知道您的js文件和您要发布到的文件必须位于同一个域吗?否则,不允许AJAX向其发出请求。你访问同一网站上的文件吗?啊,我看到你使用的是本地文件。使用行
jQuery.support.cors=true
在你的js文件中允许来自jQuery的远程请求。嘿,lukas,我的url现在已关闭,我要编辑我从GitHub获得的url,然后我希望按钮只显示页面上的第一个名称!还有另一个标签上的昵称!编辑问题
jQuery.support.cors = true;
function Getdata() {
var request = $.ajax({
type: "POST",
data: "{}",
dataType: 'json',
url: "data/json.txt", // better use a relative url here
mimeType: "application/json; cherset=utf-8",
success: loadpage,
failure: givealert
});
function loadpage(result) {
// this only displays you the values in a messagebox for you to check if it works
// you can remove the following two lines
alert("Name = "+result.fighters[0].name);
alert("Nickname = "+result.fighters[0].nickname);
// this changes the text
document.getElementById("routing").innerHTML = result.fighters[0].name;
}
function givealert(error) {
alert('failed!!' + error);
}
}