Upload and download data for the Ionic project

Source: Internet
Author: User
Tags node server

First, upload the data

Remember to inject $http dependency into the ANGULARJS controller.

var data = {ID: $scope. task_id,          groupId: $scope. Task_groupid,          importance: $scope. Priority_level,          ClassName: $scope. ClassName,          title: $scope. Task_title,          Date: $scope. Todo_date,          Detail: $scope. task_ Detail,         images: $scope. Images_list,         name: $scope. Name,         record: $scope. Recordpath,         donedate:$ Scope.done_date}                 $http ({         method: ' POST ',         URL: ' http://localhost:3000 ',         params:{         ' Require ': Data         }                  }). Success (function (Data,status,headers,config) {                  }). Error (Function (Data,status, Headers,config) {                  });


The data parameter in the $http method is not successfully received by the server

the params parameter in the $http method , the server receives the success

The server-side receive method is Url.parse (Req.url, true). Query.params

(params is the parameter added after the URL?)


Second, then download the data


    Refresh $scope. Refresh = function () {$http. Get ("Http://localhost:3000/refresh"). Success (function (data, status,                Headers, config) {//This callback'll be called asynchronously//If the response is available        Alert ("Success");                                                                                                                                                                                            Alert ("[" +data.substring (0,data.lastindexof (",")) + "]"); Todolistservice.deleteandaddtask (Json.parse (        "[" +data.substring (0,data.lastindexof (",")) + "]"); Todolistservice.findbygroupid ($stateParams. groupId). Then (function (todolists) {$scope. todolists = todolists;//corresponding category                All data list}); }). Error (Function (data, status, headers, config) {//called asynchronously if an error occurs//or Ser Ver returns response with an ERror status.        Alert ("Error" +data);        });               . Success (Function (response) {////Console.log (response);//alert (response);/}); }

The data data returned in the success method of the callback, that is, for the server response to the client

Note: The JSON string returned is guaranteed to be in JSON format (the first outermost layer without parentheses will cause an error)


Third, the service-side code

var http = require (' http '), var url = require (' URL '), var util = require (' util '); var fs = require (' FS ');    Http.createserver (function (req, res) {var urldata = req.url+ "";   var require;console.log (' connet '), if (urldata.substring (1) = = "Refresh") {Console.log ("refresh");   require = "";     Fs.readfile (' writefile.txt ', ' UTF8 ', function (err, data) {if (err) {console.error (err);           } else {console.log (data);    Res.end (data+ "");       }        });       }else{console.log ("add");   require = Url.parse (Req.url, True). Query.require+ ",";    Fs.open (' Writefile.txt ', ' a ', function opend (err, FD) {if (err) {console.error (err);    Return } var writebuffer = new Buffer (require), bufferposition = 0, bufferlength = writebuffer.length, fileposition = Nu   ll           Fs.write (FD, WriteBuffer, bufferposition, bufferlength, fileposition, Function wrote (Err,written) {   if (err) {throw err;} Console.log (ERR);       }); });}}). Listen (3000);

There's one more question.

Since Ionic has its own service to start the project, it starts the http://localhost:8100 service, and the Ionic Project Access server requires an additional port

So when the Computer browser debugging will be error

XMLHttpRequest cannot load Http://localhost:3000/refresh. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' http://localhost:8100 ' is therefore not allowed access.

You need to let the browser ignore cross-domain issues, you can simply turn off all security policies, of course, the browser will prompt you for stability and security degradation

If you do not want to use the command line each time, you can also add the startup parameters to the browser shortcut: Right-click on the Chrome icon, in the "Shortcut" tab, "Target" column to add the start parameter to the end (leave a space)


Project address (including Ionic project and simple node server code, server code used only for Server.js and Writefile.txt)

http://download.csdn.net/detail/superjunjin/8487079

The original project came from http://rensanning.iteye.com/blog/2072034


Upload and download data for the Ionic project

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.