1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

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

Discussion in 'B4J Questions' started by astronald, Jul 3, 2019.

Tags:
  1. astronald

    astronald Member Licensed User

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

    Code:
    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

    in the jserver handler, i put this line

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

    but I do not succeed

    Thanks for your help.

    Note: jserver with postman works perfect.
     
  2. Kiffi

    Kiffi Well-Known Member Licensed User

    just insert this:

    Code:
    fo.Mode = "no-cors"
    Greetings ... Peter
     
  3. alwaysbusy

    alwaysbusy Expert Licensed User

    Also my first thought, but not sure you can use 'application/json' in this mode (I can be wrong)
     
  4. astronald

    astronald Member Licensed User

    Code:
    fo.Mode = "no-cors"
    Thank but
    with this line server is empty response..
     
  5. astronald

    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?
     
  6. Kiffi

    Kiffi Well-Known Member Licensed User

    i get:
     
  7. astronald

    astronald Member Licensed User

    Thanks peter, this is a example,
    I developed procedure 'usp_Conect'
    try it again


    This should response 'OK'
     
  8. Kiffi

    Kiffi Well-Known Member Licensed User

     
  9. astronald

    astronald Member Licensed User

    Yes this is response
     
  10. Kiffi

    Kiffi Well-Known Member Licensed User

    ok, that seems to solve the problem. Or did I get you wrong?
     
  11. astronald

    astronald Member Licensed User

    No Peter thank's
    @alwaysbusy thanks, i remove Content Header
    and this worked for me
     
  12. astronald

    astronald Member Licensed User

    Final Code
    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice