Hi Aswin,I tried with your api but i didnt response so used "https://www.trackip.net/ip?json" api insteadMake sure before you execute ,create a remote site for the above urlHere is the code Lightning component :
Lightning Js:<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>
({
doInit : function(component, event, helper) {
helper.makeCallout(component);
}
})
Lightning Js Helper :
Controller :({
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);
}
})
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
2 件の回答