Nodejs get and post communication with ActionScript

Source: Internet
Author: User

Today the project encountered a small problem, did a small function to send a GET request to the server, but the service side parsing data is the time to report a syntax error, as follows:

Make Request data error:SyntaxError:Error #1132: Invalid JSON parsing input.
Error #2044: unhandled error:. Text=make Request Data error:SyntaxError:Error #1132: Invalid JSON parsing input.

Obviously, the cause of the error is that the server has interpreted this data as JSON format, but I sent the format binary, so it caused an error.

The service side of the brother asked me to send several times to request the address to him, when he was sent to him that the parameters of the GET request should be such url?key=value&

To prove to him that the GET request is not necessarily the case, I wrote a small demo of Nodejs and ActionScript to show him.

The Nodejs party code is as follows:

1 varHTTP = require (' http ');2 varFS = require (' FS ');3 varPath = require (' path ');4 varMIME = require (' MIME '));5 varQueryString = Require (' querystring '));6 varurl = require (' URL '))7 varCache ={};8 9 varPostserver = Http.createserver (function(Request, response) {TenRequest.addlistener ("Data",function(chunk) { OneConsole.log (' Get data from: ' +request.url + ' ======= Length: ' +chunk.length); AConsole.log (' Name from Request: ' +chunk); -     }); - }); the  - varGettserver = Http.createserver (function(Request, response) { -     varparams = Url.parse (Request.url,true); - Console.log (params); + Response.End (); - }); +  APostserver.listen ("8889",function(){ atConsole.log ("===========server now listenling:8889===================="); - }); -Gettserver.listen ("8887",function(){ -Console.log ("===========server now listenling:8887===================="); -});

Client ActionScript code:

1<?xml version="1.0"encoding="Utf-8"?>2<s:application xmlns:fx="http://ns.adobe.com/mxml/2009" 3xmlns:s="Library://ns.adobe.com/flex/spark" 4xmlns:mx="LIBRARY://NS.ADOBE.COM/FLEX/MX"Minwidth="955"minheight=" the"Creationcomplete="OnInit ()">5<fx:Declarations>6<!--place non-visual elements (e.g., services, value objects) here---7</Fx:declarations>8<fx:Script>9<![cdata[Ten             Private varrequest:urlrequest; One             Private varLoader:urlloader; A             Private varUrldata:urlvariables; -             Private functionOnpostclicked ():void -             { the                 //TODO Auto Generated method Stub -Request.method =Urlrequestmethod.post; -Request.url ="http://localhost:8889"; -Urldata.name ="POST"; + dorequest (); -             } +              A             Private functionOngetclicked ():void at             { -                 //TODO Auto Generated method Stub -Request.method =Urlrequestmethod.get; -Request.url ="http://localhost:8887"; -Urldata.name ="GET"; - dorequest (); in             } -              to             Private functionDoRequest ():void +             { - loader.load (request); the             } *              $             Private functionOnInit ():voidPanax Notoginseng             { -                 //TODO Auto Generated method Stub theRequest =Newurlrequest (); +Loader =NewURLLoader (); AUrldata =Newurlvariables (); theRequest.data =Urldata; +Loader.dataformat =urlloaderdataformat.binary; - Loader.addeventlistener (Event.complete, oncomplete); $ Loader.addeventlistener (Ioerrorevent.io_error, onError); $Security.allowdomain ("*"); -             } -              theProtectedfunctionOnComplete (event:event):void -             {Wuyi                 //TODO auto-generated Method Stub theTrace"Success ..."); -             } Wu              -ProtectedfunctionOnError (event:ioerrorevent):void About             { $                 //TODO auto-generated Method Stub -Trace"Fail ..."); -             } -]]> A</Fx:script> +<s:button label="SEND GET"click="ongetclicked ()"Width=" the"height="a"x="Ten"y="Ten"/> the<s:button label="SEND POST"click="onpostclicked ()"Width=" the"height="a"x="Ten"y="a"/> -</S:application>

Client:

Clicking the Send Get button will request the NODEJS server in Get mode,

Clicking the Send Post button sends the request to the NODEJS server as a post.

The results of the service end are as follows:

This is the result of receiving a GET request and a POST request once.

At first, I wrote the processing of the post request and the processing of the GET request to the same handler, but the GET request succeeds, the console does not print the information, and for what reason, the port of the GET request is separated from the port of the POST request. The Get port prints the relevant content.

Temporarily do not know what the reason, after the follow-up study will update.

UPDATE: Special Meow, the discovery is actually a client-side parsing problem, the result returned by the server is not formatted with JSON.

Nodejs get and post communication with ActionScript

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.