简体   繁体   English

使用AS3将数据发送到JSON

[英]Sending data to a JSON with AS3

I've asked my client to share his database login and password but he can't give me full access to his database (security reason I suppose). 我已经要求客户共享他的数据库登录名和密码,但是他不能给我完全访问他的数据库的权限(我想是出于安全原因)。 He told me to use a REST/JSON service that allows to post the data via this url with a specific key that allows him to identify all the datas coming from my app. 他告诉我使用REST / JSON服务,该服务允许使用具有特定密钥的此URL通过该URL发布数据,从而使他可以识别来自我的应用程序的所有数据。

Here's what I did : 这是我所做的:

var urlRequest:URLRequest = new URLRequest("the_url_using JSON service");
urlRequest.method = URLRequestMethod.POST;

 var urlvars: URLVariables = new URLVariables;
urlvars.observer_name = "Test Coco";
urlvars.observation_number = "5433";

trace("urlvars = "+urlvars);

urlRequest.data = urlvars;

var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(urlRequest);

It's working, as it's sending the data, but the data format seems to be incorrect.. 它正在发送数据,因此正在运行,但是数据格式似乎不正确。

the url returns this error : "Observer name is Missing" url返回此错误:“观察者名称丢失”

And the " trace (urlvars) " output : 和“ trace (urlvars) ”输出:

urlvars = observer%5Fname=Test%20Coco&observation%5Fnumber=5433

So I think the problem come from the special character or something like that (as you can " observer_name " results by " observer%5Fname " and we can see a lot of %5") 因此,我认为问题来自特殊字符或类似的字符(因为您可以通过“ observer%5Fname ”获得“ observer_name ”结果,并且可以看到很多“%5”)

Any idea how can I solve this ? 知道我该如何解决吗?

JSON string is a string representation of a generic object. JSON字符串是通用对象的字符串表示形式。 Basically you go: 基本上你去:

var anObject:Object =
{
    "observer_name": "Test Coco",
    "observation_number": 5433
};

or you can construct it 或者你可以构造它

var anObject:Object = new Object;

anObject['observer_name'] = "Test Coco";
anObject['observation_number'] = 5433;

and then you convert it to String and attach to request 然后将其转换为String并附加到请求

var jsonString:String = JSON.stringify(anObject);

urlRequest.method = URLRequestMethod.POST;
urlRequest.data = jsonString;

Read more about it: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html 进一步了解它: https : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/JSON.html

Keep in mind that I don't know the specifics of that REST server of yours and the code above just might not work as it is. 请记住,我不知道您所用的REST服务器的细节,上面的代码可能无法按原样工作。 I only explain how to send a JSON string as a POST request. 我仅说明如何将JSON字符串作为POST请求发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM