jquery returns JSON data instances via AJAX request Nodejs _jquery

Source: Internet
Author: User
Tags eval

Recently looked at the Nodejs related, on the Internet to check the next combination of AJAX applications, feeling the application of the future is still good. Why use this combination?

1.NodeJS do not need to install, copy the past can be used, and environment variables can only be configured in the current CMD window, easy to run.

2. Ajax requests through HTML can be implemented on different servers to obtain data across domains.

3. Communication data format is flexible, can be XML, JSON, binary, etc., data suitable for any platform.

In talking about my environment, I use the company to provide the computer, there are many restrictions, such as the domain in the computer, the operation of low permissions, can not install any software, can not modify the computer configuration, can not use U disk and so on. So I configured a Nodejs green version of the environment, only in the current CMD window to run NODEJS related environment; Unable to erect HTML server, so just create an HTML file in D disk double-click open to use.

One, node environment will not say, you can view the following several articles

Http://www.jb51.net/article/33086.htm

Http://www.jb51.net/article/54678.htm

Http://www.jb51.net/article/57687.htm

Second, write the script of node execution, script file App.js as follows:

var http = require ("http");
var fs = require ("FS");

var str= ' {"id": "123", Name: "Jack", arg:11111} ';

function onrequest (request, Response) {
 Console.log ("request received.");
 Response.writehead (200,{"content-type": ' Text/plain ', ' CharSet ': ' Utf-8 ', ' access-control-allow-origin ': ' * ', ' Access-control-allow-methods ': ' put,post,get,delete,options '})//Can resolve requests across domains
 //response.writehead (200,{" Content-type ": ' Application/json ',   ' access-control-allow-origin ': ' * ', ' access-control-allow-methods ': ' Put, Post,get,delete,options '});
 Response.Write ("Hello World 8888\n");
 
 Str=fs.readfilesync (' data.txt ');
 Response.Write (str);
 Response.End ();
}

Http.createserver (ONrequest). Listen (8888);

Console.log ("Server has started.port on 8888\n");
Console.log ("Test data:" +str.tostring ());

where data.txt and current app.js files are placed in the same directory, the code in Data.txt is JSON-formatted data: {"id": "123", Name: "Jack", Arg:321,remark: "Test Data"}

Run it through node app.js, and then you can have HTML access to the data via Ajax.

The other is the HTML file I created, and the file aaa.html reads as follows:

<! DOCTYPE html>  
 

file in the corresponding annotation notes, here is no longer explained, which use the jquery js file to download one of their own online, but also placed in the directory of the current HTML file.

A note: I downloaded jquery 1.8.3 and 2.1.4 will be an error, may not be good for JSON support, the use of 1.4.4 version of the normal operation, the other version has not been tested.

In my native and intranet test the normal display is as follows:

{"id": "123", Name: "Jack", arg:32100, remark: "Test Data"}
Item No: 123
Name: Jack
Age: 32100
Test data

The above environment is my own test, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.