I have an API in my app, how can I call the API in a JS script when I don't have a URL and I'm still in development?
            Asked
            
        
        
            Active
            
        
            Viewed 476 times
        
    -3
            
            
        - 
                    http://stackoverflow.com/questions/9922101/get-json-data-from-external-url-and-display-it-in-a-div-as-plain-text etc – Jan 30 '17 at 21:28
- 
                    @nogad I know how to process the JSON file once I get it. But I don't know how can I call my API since it's internal and I don't have a URL to pass to JS. Thanks – Heraclitus Jan 30 '17 at 21:30
- 
                    Hard to make any sense out of this question – charlietfl Jan 30 '17 at 21:31
- 
                    `it's internal` ... you mean the server isn't exposed to the internet? – Jaromanda X Jan 30 '17 at 21:31
- 
                    that link tells you HOW to get it – Jan 30 '17 at 21:32
- 
                    I'm sorry @charlietfl I probably explained it in a bad way – Heraclitus Jan 30 '17 at 21:32
1 Answers
2
            Do not specify the domain name as part of the API's URL and deploy the JavaScript application to the same domain/port as the PHP API. You may have to do this anyway for security reasons (look up CORS).
So, if you call your API endpoint at /api/endpoint from your application deployed at /app, you will be independent of the domain, no matter if you are working locally
http://localhost:8080/app
http://localhost:8080/api/endpoint
, or in production
https://example.com/app
https://example.com/api/endpoint
 
    
    
        TimoStaudinger
        
- 41,396
- 16
- 88
- 94
- 
                    I had tried this but I didn't get it working, I think I didn't put the http:// for some reason. Stupid of me. Cheers mate – Heraclitus Jan 30 '17 at 21:45
