httprequest error

Monkey Targets Forums/HTML5/httprequest error

Yoda(Posted 2014) [#1]
I use the below code for reading stored data (player name, highscores) in the game.
It works for some people, but for some others it gives me the following error in the browsers error log:

XMLHttpRequest cannot load http://www.website.com/script.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.

The request is sent from the same website, so it should work. Why it doesn't for some people?

communicate.server = New Http()
playername=communicate.sendrequest("script.php")


Class communicate
Global server:Http = Null
Global requestadress$, requestresult$

Function sendrequest$(request$)
requestadress="http://www.website.com/"
requestresult= server.Get( requestadress + request, 2000)
Return requestresult
End
End


Landon(Posted 2014) [#2]
could be a browser specific issue perhaps?

i typically use the HttpRequest Class instead

e.g.
Class LoadingCallback Implements IOnHttpRequestComplete
Field get_req:HttpRequest
Field data:String
Field finished:Bool = False

	Method OnHttpRequestComplete:Void( req:HttpRequest )
		
		
		Print "Status: "+req.Status()
		data = req.ResponseText()
		finished = True
		
		
	End

	Method Load:Void(url:String,data:String="",mime:String="text/plain;charset=utf-8",enc:String="utf8")
		fileurl = url

		        get_req=New HttpRequest( "GET",url,Self )
	        	If hasheader = True Then
	        		get_req.SetHeader(headername,headerval)
	        	Endif		        
		        If data <> "" then
		        	get_req.Send(data,mime,enc)
		        Else
		        	get_req.Send()
		        Endif		
	End
End Class


Then somewhere in my code i'll call

Local callback:LoadingCallback = New LoadingCallback
callback.Load("http://example.com/wp-admin/admin-ajax.php?action=dogetfiles","")


I make sure in my update loop somewhere i have
UpdateAsyncEvents


then i just have the program sit until the request is completed.

using this method i haven't ran into any problems on different browsers.


golomp(Posted 2014) [#3]
Hi,

I think this issue is not due to monkey (i work in V77F) nor to a specific browser.

In my case, with the same browser (i surf with Firefox 29.0.1), sometime it works and sometime not. (just a few minutes waiting betwen two tries)

So my actual solution is to adapt my scripts. (for exemple, for a few loops, create a flag file, execute da script (ending the script with file deletion), check file presence, if it's there stay in the loop, if not exit (finally the script have been executed)) If the loop limit have been raised, send a message to admin. (Me ;-)

Regards
Golomp