URLRequest/URLLoader auto-converting POST request to GET

依然范特西╮ 提交于 2019-12-18 08:31:38

问题


When I execute the following code:

var urlRequest:URLRequest = new URLRequest("http://somehost/with/some/path?andsomequerystring=true");
urlRequest.method = 'POST';
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE,  function(event:Event):void{
    trace('sweet');
});

It turns my explicit POST request to GET due to the presence of the querystring. If I remove the querystring (and serialize as part of the POST body), it successfully makes the corresponding POST request. Is there any way to prevent it from doing that? My server requires that a POST request be made with a querystring.


回答1:


Ah think I found the answer, seems you have to specify a body as well or else it will still send as a GET request from their docs:

Note: If running in Flash Player and the referenced form has no body, Flash Player automatically uses a GET operation, even if the method is set to URLRequestMethod.POST. For this reason, it is recommended to always include a "dummy" body to ensure that the correct method is used.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html#url




回答2:


It is because the way you are having your url. If you want to have your variables for POST method you need to use URLVariables.

var urlRequest:URLRequest = new URLRequest(YOUR_REQUEST_URL_HERE);
var variables:URLVariables = new URLVariables();
variables.andsomequerystring = true;

urlRequest.data = variables;
urlRequest.method = 'POST';
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE,  function(event:Event):void{
    trace('sweet');
});


来源:https://stackoverflow.com/questions/12774611/urlrequest-urlloader-auto-converting-post-request-to-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!