如何使用jquery ajax和jsonp在您自己的域上读取json数据
How to read json data on your own domain using jquery ajax and jsonp
我有这段代码,我想读取我域上的JSON数据,但我已经尝试了所有我能想到的,但无法获得。
这是代码:
<script>
$(function () {
$.ajax({
"url": "http://www.mmsbip.com.ng/accounts.json",
"type": "get",
"dataType": "json"
})
.done(function (data) {
var options = $("#users");
$.each(data, function (val) {
alert(data.UserID);
}));
});
})
.fail(function (jqXHR, status, error) {
console.log("status:", status, "error:", error);
})
});
我的服务器上accounts.json中的数据是
[
{
"UserID": "timotech",
"UserName": "timotech@yahoo.com",
"Password": "password"
},
{
"UserID": "teslim",
"UserName": "teslimbakare@live.com",
"Password": "password"
},
{
"UserID": "bola",
"UserName": "bettyolabode@yahoo.com",
"Password": "password"
}
]
如何从服务器获取这些数据
编辑:我决定把整页都贴出来,这样你就可以看到我犯了什么错误。感谢
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple JSON Example</title>
<script src="jmobile/jquery-2.1.3.js"></script>
<script>
$(function () {
$.ajax({
"url": "http://www.mmsbip.com.ng/accounts.json",
"type": "get",
"dataType": "json"
})
.done(function (data) {
var options = $("#users");
$.each(data, function (val) {
alert(data.UserID);
});
})
.fail(function (jqXHR, status, error) {
console.log("status:", status, "error:", error);
})
});
</script>
</head>
<body>
<h2>Simple JSON Example</h2>
<p>This DropDown is populated using $.ajax()</p>
<select name="users" id="users" />
</body>
</html>
该警报只是为了测试是否返回json数据。感谢
代码中有几个小错误。试试这个:
$(function () {
$.ajax({
"url": "http://www.mmsbip.com.ng/accounts.json",
"type": "get",
"dataType": "json"
})
.done(function (data) {
var options = $("#users");
$.each(data, function (val) {
alert(data.UserID);
});
})
.fail(function (jqXHR, status, error) {
console.log("status:", status, "error:", error);
})
});
您过早地(在警报之后)关闭了原始函数,并且代码中有一个额外的});
。
相关文章:
- 按我自己的类克隆另一个元素的内容和顺序
- 如何配置分析以将数据发送到我自己的服务器
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- 创造自己的智能
- 如何在Win8Metro应用程序的Javascript代码中捕获自己的C#事件
- 从自己的安全系统中重新找回自己
- 麻烦将coffee脚本片段移动到rails中自己的类中
- 从我自己的脚本中包含的Javascript文件中调用一个函数
- 让我自己的过滤器不工作
- 消隐数组元素是否生成自己的属性
- JavaScript子对象访问自己的值
- Selenium异步脚本在自己的线程中阻塞其他脚本
- 为什么对象的函数值没有自己的作用域
- 我怎样才能给数组中的每个对象赋予它们自己的键
- 制作我自己的加密/解密系统
- 我如何才能使数组中的按钮只对其作出响应'使用javascript的自己的包装器
- NodeJS:一个全局EventEmitter或每个模块它'他自己的
- 如何将我自己的按钮连接到下载库
- PHP脚本在Lion上通过Ajax运行时会回显自己的内容
- 当我使用Extjs 5中的一个树示例来制作我自己的树时.我将获得一个无法读取的属性'项目'为null