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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…