通过nodejs从客户端请求JSON数据
Requesting JSON data from client through nodejs
我的服务器上有一个文件(json格式)somefile.json
,它会在选定的时间间隔定期更新。
我有一个nodeJS应用程序,它读取在8080端口侦听的request
事件上的文件,并将其作为response
发送出去
如何在html中获取javascript来请求json数据并将其记录在控制台中?(尝试但失败,见下文)
(console.log的原因是让我知道它已经成功加载。)
我的nodeJS应用
var http = require('http'),
fs = require('fs'),
filename = "somefile.json";
var server = http.createServer();
server.on('request', function(request, response) {
response.writeHead(200, {'Content-Type': 'application/json'})
fs.readFile(filename, "utf8", function (err, data) {
if (err) throw err;
response.write(JSON.stringify(data));
response.end();
});
});
server.listen(8080);
somefile.json
{
"message": {
"success":"Information inserted successfully.", "update":"Information updated successfully.",
"delete":"Information deleted successfully.",
},
"Jennifer": {
"status":"Active"
}, "James": {
"status":"Active",
"age":56, "count":10,
"progress":0.0029857,
"bad":0
}
}
在我的本地机器(OSX)上使用cURL给了我以下信息:
$ curl -i -H "Accept: application/json" http://127.0.0.1:8080
HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 19 Sep 2014 03:39:41 GMT
Connection: keep-aliveTransfer-Encoding: chunked
"{'n '"message'": {'n '"success'":'"Information inserted successfully.'",'n '"update'":'"Information updated successfully.'",'n '"delete'":'"Information deleted successfully.'",'n },'n '"Jennifer'": {'n '"status'":'"Active'"'n },'n '"James'": {'n '"status'":'"Active'",'n '"age'":56,'n '"count'":10,'n '"progress'":0.0029857,'n '"bad'":0'n }'n}'n"
我的html(不工作)
<html>
<head></head>
<body>
<script src="jquery-2.1.1.min.js"></script>
<script>
$(function() {
$.ajax({
url: 'http://127.0.0.1:8080',
contentType:"jsonp",
dataType: 'jsonp',
cache: false,
success: function() { console.log('Success!'); },
error: function() { console.log('Uh Oh!'); }
});
});
</script>
</body>
</html>
您可以对后端API进行AJAX调用,它需要返回JSONP格式,而不仅仅是JSON,否则会出错。这是由于同源政策:
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy.
此讨论可能有助于理解JSONP:
有人能用外行的话解释什么是JSONP吗?
然而,一个替代方案是禁用谷歌Chrome浏览器的安全性,然后它就会工作。但这不是一个解决方案。您需要使用JSONP格式。
因为您使用一个html源,另一个json源。这被视为跨域请求。
1-像callbackname({data: value})
这样封装JSON对象。
var http = require('http'),
fs = require('fs'),
filename = "somefile.json";
var server = http.createServer();
server.on('request', function(request, response) {
response.writeHead(200, {'Content-Type': 'application/json'})
fs.readFile(filename, "utf8", function (err, data) {
if (err) throw err;
response.write("callbackname(");
response.write(data);
response.write(")");
response.end();
});
});
server.listen(8080);
2-将jsonpCallback: "callbackname"
添加到您的ajax请求中。
<html>
<head></head>
<body>
<script src="jquery-2.1.1.min.js"></script>
<script>
$(function() {
$.ajax({
url: 'http://127.0.0.1:8080',
jsonpCallback: "callbackname",
contentType: "application/json",
dataType: 'jsonp',
cache: false,
success: function(json){
console.log(json);
}
});
});
</script>
</body>
</html>
相关文章:
- 如何将JSON数据导入我的ejs模板
- 如何将json数据显示为html
- Json数据包含日期和时间格式
- 用我的json数据填充JQuery DataTable
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 使用jquery读取Json数据
- 如何将本地json数据加载到Extjs数据模型中
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- React-将jSON数据传递给子组件的子组件
- 尝试从控制器加载带有json数据的Simile时间线
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何在ReactJs中显示Json数据
- 使用javascript进行JSON数据检索
- tu如何将id放在填充了json数据的html表td上
- 将JSON数据从服务器加载到knockout.jsobservable中
- 使用JavaScript处理JSON数据
- 如何将json数据传递到.js文件
- 如何在带有nunjucks和gullow数据的JSON数据中使用带有HTML的内容
- 如何使用var名称调用JSON数据