B4J Question [BANANO] [SOLVED] fetch api cors error?

astronald

Member
Licensed User
i try connect banano to jserver 0n other domain, with this code:

B4X:
    Dim response As BANanoFetchResponse
    Dim data As String
    Dim fo As BANanoFetchOptions
    fo.Initialize
    fo.Method = "POST"
    fo.Body = BANano.ToJson(CreateMap("q":"Conect","p":Array(),"l":0))
    fo.Headers = CreateMap("Content-type": "application/json; charset=UTF-8")
   
    Dim fetch As BANanoFetch
    fetch.Initialize("http://rocas.noip.us/tmx",fo)
    fetch.Then(response)
    fetch.Return(response.Text)
   
    fetch.Then(data)
        Log(data)
    fetch.End ' always end a fetch with this!
i receive this error message

Access to fetch at 'http://rocas.noip.us/tmx' from origin 'http://127.0.0.1:8887' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
in the jserver handler, i put this line

B4X:
resp.SetHeader("Access-Control-Allow-Origin","*")

but I do not succeed

Thanks for your help.

Note: jserver with postman works perfect.
 

astronald

Member
Licensed User
if i disable cross origin restrictions on browser, this work properly,
but this is not right.

How should I configure the server and make the request?
 

Kiffi

Well-Known Member
Licensed User
i get:
Failed to load resource: the server responded with a status of 500 (java.sql.SQLException: Could not find stored procedure 'usp_Conect'.)
 

astronald

Member
Licensed User
i get:
Failed to load resource: the server responded with a status of 500 (java.sql.SQLException: Could not find stored procedure 'usp_Conect'.)
Thanks peter, this is a example,
I developed procedure 'usp_Conect'
try it again


This should response 'OK'
 

astronald

Member
Licensed User
Final Code
B4X:
Dim response As BANanoFetchResponse
    Dim data As String
    
    Dim fo As BANanoFetchOptions
    fo.Initialize
    fo.Method = "POST"
    fo.Body = BANano.ToJson(CreateMap("q":"Conect","p":Array(),"l":0))

    
    ' list (GET is default, and we do not need extra options so we pass Null for the options)
    Dim fetch As BANanoFetch
    fetch.Initialize("http://rocas.noip.us/tmx",fo)
    fetch.Then(response)
    ' we got a response, but as the Json() method returns a Promise, we will need to process it in the next 'then' so we return it to this Fetch
    fetch.Return(response.Text)
    
    fetch.Then(data)
    ' the Json promise is resolved, lets log it...
   Log(data)
fetch.End
 
Top