从Javacsript读取响应

时间:2018-12-26 05:22:26

标签: javascript get salesforce apex visualforce

我正在尝试从Visualforce页面调用Javascript。以下是visualforce页面的示例代码。在这里,我试图调用get方法。我想看看回应。它会保存在哪里?在开发者控制台中单击预览后,将执行JS。在哪里可以看到回复?

<apex:page controller="calljavascript_cls" >

<script>

function func()

{

 var request = new XMLHttpRequest();
 var org_id = '***';
 var user_api_key = '******';

 request.open('GET', 'https://copado.herokuapp.com/json/v1/webhook/metadata/'+org_id+'?api_key='+user_api_key);

 request.onreadystatechange = function () {
 if (this.readyState === 4) {
 console.log('Status:', this.status);
 console.log('Headers:', this.getAllResponseHeaders());
 console.log('Body:', this.responseText);
 }
 };

 request.send();
 }

  </script>

   <apex:outputText value="{!callfunc}" escape="false"></apex:outputText>

  </apex:page>

下面的代码是JavaScript。

public class calljavascript_cls

{

public string callfunc{get;set;}
public calljavascript_cls()

{

 callfunc='<script> func(); </script>';

 }

 }

2 个答案:

答案 0 :(得分:0)

在这里尝试:

  request.onreadystatechange = function () {
      if (request.readyState === 4) {
           if (request.status == 200) {
               console.log('Status:', request.status);
               console.log('Headers:', request.getAllResponseHeaders());
               console.log('Body:', request.responseText);
           }
           else if (request.status == 400) {
              alert('There was an error 400');
           }
           else {
               alert('something else other than 200 was returned');
           }
       }
   }

答案 1 :(得分:0)

您将在

中得到答复
  request.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       /* Returns a DOMString that contains the response to the request as text, 
        or null if the request was unsuccessful or has not yet been sent.
       */
       console.log(this.responseText);

       /*
         Returns an ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, 
         depending on the value of XMLHttpRequest.responseType. that contains the 
         response entity body.
       */
       console.log(this.response);
    }
    else
    {
       console.log(this.status); 
    };

有关更多信息,请查看https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest