【问题标题】:Action Script 3 - Posting a JSON to a RESTful server动作脚本 3 - 将 JSON 发布到 RESTful 服务器
【发布时间】:2014-11-20 16:25:15
【问题描述】:

我正在尝试使用以下代码将一个简单的 JSON 对象发布到 RESTfull 服务器:

var messages:Array = new Array ();  
messages.push ({"name":"MyName"});

var vars: URLVariables = new URLVariables();
vars.data   = JSON.stringify(messages);

var urlRequest:URLRequest= new URLRequest("http://localhost:8080/xxx/player/createAccount");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = vars;
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
urlRequest.requestHeaders.push(hdr);

_urlLoader = new URLLoader();
_urlLoader.addEventListener(Event.COMPLETE, onXMLDataLoaded);
_urlLoader.load(urlRequest);

我的对象是一个简单的对象,它包含一个名为 {"name" : "MyName"}

服务器无法识别请求的数据。

网络监视器上的请求显示:

POST http://localhost:8080/xxx/player/createAccount HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 42
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080
X-Requested-With: ShockwaveFlash/15.0.0.223
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/39.0.2171.65 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: http://localhost:8080/xxx/flashClient/lobby.swf
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,he;q=0.6
Cookie: JSESSIONID=1841C3CBE7511794A4EEF8A1A0BD56DD

data=%5B%7B%22name%22%3A%22MyName%22%7D%5D

网络监控工具上的工作发布请求如下所示:

POST http://localhost:8080/xxx/player/createAccount HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 20
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)    Chrome/39.0.2171.65 Safari/537.36
Origin: chrome-extension://cdjfedloinmbppobahmonnjigpmlajcd
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,he;q=0.6
Cookie: JSESSIONID=1841C3CBE7511794A4EEF8A1A0BD56DD

{ "name" : "MyName"}

任何想法如何让第一个请求像第二个请求一样执行?

【问题讨论】:

  • 你是如何生成第二个 POST 请求的?
  • 感谢您通知我,我忘了添加我的代码:) 我编辑了帖子。
  • 问题是您将 JSON 转换为字符串,然后将该字符串填充到 URLVariables 对象中,然后再对字符串进行编码。您不希望对字符串 JSON 进行编码,因此只需将请求的数据属性直接分配为字符串化 JSON。 (我认为这就是@Crabar 的答案试图解释的)。

标签: ajax json actionscript-3 flash apache-flex


【解决方案1】:

如果您的内容类型是application/json,那么您不想将URLVariables 用于您的数据:

//this is causing the problem because it's encoding your JSON string so it's url safe.
var vars: URLVariables = new URLVariables();
vars.data   = JSON.stringify(messages);

相反,将字符串化的 JSON 直接分配给 URLRequestdata 属性,如下例所示:

var urlRequest:URLRequest= new URLRequest("http://localhost:8080/xxx/player/createAccount");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = JSON.stringify({"name":"MyName"});
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
urlRequest.requestHeaders.push(hdr);

_urlLoader = new URLLoader();
_urlLoader.addEventListener(Event.COMPLETE, onXMLDataLoaded);
_urlLoader.load(urlRequest);

附:有一个很好的 tutorial 可以帮助理解 REST 范式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多