Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

Word add-in with XMLHttpRequest()

I'm new to Office add-ins so am playing around with the MS tutorials on add-ins for Word. I want to grab the echo statement of a PHP file (http://localhost/wikindx6/trunk/office.php) in the add-in. I have XAMPP running on default localhost where my PHP file is located. The code for that is (and, in a web browser, I get the expected echo printed):

<?php
        echo json_encode("HERE I AM");
        die;
?>

The add-in runs on localhost:3000 (default values when set-up). I've therefore added this to manifest.xml in the hope this will access the PHP file:

  <AppDomains>
    <AppDomain>http://localhost</AppDomain>
  </AppDomains>

The javascript business end of the add-in is (it inserts text at the cursor in the word document):

function insertText() {
  Word.run(function (context) {
    var selection = context.document.getSelection();
    var returnText = 'start';
    var xml = new makeHttpObject();
    xml.onreadystatechange = function() {
        if( xml.readyState == 4 && xml.status == 200 ){
          returnText = xml.responseText;
        } else {
          returnText = 'failure';
        }
    };
    xml.open("GET", "/wikindx6/trunk/office.php", false);
    xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xml.send("format=json");
      var text = selection.insertText(returnText, "After");
    return context.sync();
  })
  .catch(function (error) {
      console.log("Error: " + error);
      if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
      }
  });
}

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

I would expect to get 'HERE I AM' printed but instead get 'failure'. Thanks in advance for any help.

question from:https://stackoverflow.com/questions/65872230/word-add-in-with-xmlhttprequest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...