Skip to main content
Ashwin KC (PwC (NZ)) 님이 #Lightning Experience에 질문했습니다
Hi 

I have scnerio where I need to capture IP address of current User in Lightning Community, is any way standard way  where I can capture IP address inside Lightning Components.

I have seen third party tools like - https://www.ipify.org/ to get Ip address 

Any recommendations are appreciated 

Thanks 
답변 2개
  1. 2018년 1월 30일 오전 7:28
    Hi Aswin,

    I tried with your api but i didnt response so used "https://www.trackip.net/ip?json" api instead

    Make sure before you execute ,create a remote site for the above url

    Here is the code

    Lightning component :

    <aura:component controller="demoCalloutController">

    <aura:attribute name="responseTextValue" type="String" default="hello" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    {!v.responseTextValue}

    </aura:component>

    Lightning Js:

     

    ({

    doInit : function(component, event, helper) {

    helper.makeCallout(component);

    }

    })

    Lightning Js Helper :

    ({

    makeCallout : function(component) {

    var action = component.get("c.getIpValue");

    action.setParams({});

    // Create a callback that is executed after

    // the server-side action returns

    action.setCallback(this, function(response) {

    var state = response.getState();

    if (state === "SUCCESS") {

    // Alert the user with the value returned

    // from the server

    // alert("From server: " + response.getReturnValue());

    component.set("v.responseTextValue",response.getReturnValue());

    // You would typically fire a event here to trigger

    // client-side notification that the server-side

    // action is complete

    }

    else if (state === "INCOMPLETE") {

    // do something

    }

    else if (state === "ERROR") {

    var errors = response.getError();

    if (errors) {

    if (errors[0] && errors[0].message) {

    console.log("Error message: " +

    errors[0].message);

    }

    } else {

    console.log("Unknown error");

    }

    }

    });

    // optionally set storable, abortable, background flag here

    // A client-side action could cause multiple events,

    // which could trigger other events and

    // other server-side action calls.

    // $A.enqueueAction adds the server-side action to the queue.

    $A.enqueueAction(action);

    }

    })

    Controller :

     

    public class demoCalloutController {

    @AuraEnabled

    public static String getIpValue() {

    HttpRequest req = new HttpRequest();

    req.setMethod('GET');

    //Set HTTPRequest header properties

    req.setHeader('content-type', 'application/json');

    req.setEndpoint( 'https://www.trackip.net/ip?json');

    //Set the HTTPRequest body

    // req.setBody();

    Http http = new Http();

    HTTPResponse res=null;

    try {

    //Execute web service call here

    res = http.send(req);

    //Helpful debug messages

    System.debug(res.getBody());

    System.debug('STATUS:'+res.getStatus());

    System.debug('STATUS_CODE:'+res.getStatusCode());

    }

    catch(System.CalloutException e) {

    //Exception handling goes here....

    }

    return res.getBody();

    }

    }

    Lightning App:

    <aura:application >

    <!-- <shivaKalinga:LightningImageComponent ></shivaKalinga:LightningImageComponent>

    -->

    <shivaKalinga:ApiAccess ></shivaKalinga:ApiAccess>

    </aura:application>

    Let me know if you need further help.

    Thanks and Regards,

    Shiva RV
0/9000