function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sony sonusony sonu 

getting response in anonymous window and not getting response in vf page

public class OrgRestDemoController2 {

   

           public static void submit() {

HttpRequest request = new HttpRequest();
request.setEndpoint('http://.sfdkcimmmn.com/api/responsexxx');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
string Str ='{"Name": "ddu","Type": "xxx", "PayFrequency": "0R1jIbo/lZquTYoMRtljVw=="}';
        
request.setBody(str);
//request.setCompressed(true);
Http http = new Http();
HTTPResponse response = http.send(request);
// Parse the JSON response

if (response.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
                 response.getStatusCode() + ' ' + response.getStatus());

else  
{
    System.debug(response.getBody());
}
}
}
===============================================================
<apex:page controller="OrgRestDemoController2" showHeader="false">


<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton action="{!submit}" value="submit" />
</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>


on clicking on submit button, iam not getting the blank page,,,,, bt in anonymous window it got executed,, may in know my method is correct or wrng please...or i should use page reference method instead of the submit??? can please rewrite the code ..to executeit thanks in advance...
Best Answer chosen by sony sonu
<Saket><Saket>
Hi you can contact me on this email id -> sharmasaket703@gmail.com

Please mark best answer from the above answers if your problem get solved

Thank you.

All Answers

sony sonusony sonu
i should get the json response in the vf page, please let me know , where i had done the mistake in apex clas or the vf page
GhanshyamChoudhariGhanshyamChoudhari
apex
 
public class OrgRestDemoController2 {
String responsejson {get;set;}
   

           public static void submit() {

HttpRequest request = new HttpRequest();
request.setEndpoint('http://.sfdkcimmmn.com/api/responsexxx');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
string Str ='{"Name": "ddu","Type": "xxx", "PayFrequency": "0R1jIbo/lZquTYoMRtljVw=="}';
        
request.setBody(str);
//request.setCompressed(true);
Http http = new Http();
HTTPResponse response = http.send(request);
// Parse the JSON response

if (response.getStatusCode() != 200) {
    System.debug('The status code returned was not expected: ' +
                 response.getStatusCode() + ' ' + response.getStatus());
} 
else  
{
    System.debug(response.getBody());
	responsejson = response.getBody();
}
}
}

vf
<apex:page controller="OrgRestDemoController2" showHeader="false">


<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
response in json : {!responsejson}
<apex:commandButton action="{!submit}" value="submit" />
</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>
</apex:page>

 
<Saket><Saket>
I think you are getting this error

User-added image
First add remote site setting for this url and most important thing your Endpoint url is in wrong format there is one dot extra in it so please have a look correct formatted url is this http://sfdkcimmmn.com/api/responsexxx paste this url in remote site setting.

If still your problem is not solved then let me know and please also upload image of what u r getting after calling acion.

Thanks
Saket Sharma
sony sonusony sonu
hi 
GhanshyamChoudhari, thanks for your reply, iam getting the error,please let me know if you have any idea
User-added image
 
sony sonusony sonu
hi Saket Sharma 30,
thanks for your help...
i had done the remote site setting and it is correct url... i got response in anonymous window,,,, actually i had not shared the corect url due to privacy..


sony sonusony sonu
iam getting the errror,,variable doesnot exist
<Saket><Saket>
Hi Sfdc Community

Thanks for information u shared ,
as your submit method is static so for this you to make responsejson variable is to be static or second approach can be to remove static keyword from the submit method.
and if you want to refrence this responseJson in VF page then make it public also.
Hope it wil work, if your problem get solved then please mark my answer as a best answer.

Thanks 
GhanshyamChoudhariGhanshyamChoudhari
static String responsejson {get;set;}

 
sony sonusony sonu
hi GhanshyamChoudhari, iam sory to tel this ...i had got the same error..thanks for your help
GhanshyamChoudhariGhanshyamChoudhari
<apex:page controller="OrgRestDemoController2" showHeader="false">
<apex:form >
<apex:pageBlock >
response in json : {!responsejson}
<apex:pageBlockButtons >

<apex:commandButton action="{!submit}" value="submit" />
</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>
</apex:page>

 
<Saket><Saket>
<apex:page controller="OrgRestDemoController2" showHeader="false">


<apex:form >
<apex:pageBlock >
response in json : {!responsejson}
<apex:pageBlockButtons >

<apex:commandButton action="{!submit}" value="submit" />
</apex:pageBlockButtons>

</apex:pageBlock>

</apex:form>
</apex:page>

Use this vf code Snippet and I think it will work, If your problem get solved then please mark it as a best answer.

Thanks 
Saket Sharma
sony sonusony sonu
User-added image
hi Saket  &
GhanshyamChoudhari , thanks for your support, iam sorry, to say that iam getting the above error please 
<Saket><Saket>
This error is in controller please check this in line 5 or share the current controller code?
<Saket><Saket>
<apex:page controller="CheckContr">
<apex:form >
  <apex:pageBlock >
    {!var}
    <apex:pageBlockButtons rendered="{! responseJson==null}"> 
    <apex:commandButton action="{!call}" value="Submit" />
    </apex:pageBlockButtons>

</apex:pageBlock>
</apex:form>
</apex:page>
Use this and let me know if it working or not
 
sony sonusony sonu
hi saket,, thanks for ur replyUser-added image
<Saket><Saket>
I m sorry use this below :P
<apex:page controller="CheckContr">
<apex:form >
  <apex:pageBlock >
    {!responseJson}
    <apex:pageBlockButtons rendered="{! responseJson==null}"> 
    <apex:commandButton action="{!call}" value="Submit" />
    </apex:pageBlockButtons>

</apex:pageBlock>
</apex:form>
</apex:page>


 
sony sonusony sonu
hi saket & GhanshyamChoudhari  ,,,,and awsemethanks for your respnse and suppor,, yes i got the out put as per my requirment ...next step to deserialize it according to the json response with respect to all fields and subfields...may i get your skypeid saket &  GhanshyamChoudhari   if possible..
<Saket><Saket>
Hi you can contact me on this email id -> sharmasaket703@gmail.com

Please mark best answer from the above answers if your problem get solved

Thank you.
This was selected as the best answer