As I have known AJAX in the past, it has gone something like this: a server side language (in my case PHP) file generates xHTML has JS attached. The JS queries another file, with parameters set in GET
or POST
. This queried file's entire output is returned to the JS, which in turn makes a change on the original xHTML.
Now, I'm trying to do something which is sorta related to those same techniques. I want to query URLs from Excel, like
www.example.com/distance?to=FTW&from=ACR
and the entire return value would be something like
2231
If I just query the JS file directly, it 开发者_运维百科won't get run.
If I set up an html page, it's trivial to have it display nothing but that number, but the return value of the query contains a bunch of tags.
How else can I do this? What if I had a PHP file, that runs the JS, then returns its value?
The business logic of getting the number has to be in javascript.
Simply add a callback function to handle the value
in your main file:
<script type="text/javascript">/* <![CDATA[ */
function valcallback(val){
// so something with val
}
/* ]]> */</script>
in your JS file, return:
valcallback(2231);
and it'll run like javascript.
The Excel request side can be handled with this VBA code.
Sub GetDistance()
Dim objXML As Object
Dim strData As String
Dim strResponse As String
strData = "to=FTW&from=ACR"
Set objXML = CreateObject("MSXML2.XMLHTTP")
objXML.Open "POST", "www.example.com/distance?" & strData, False
objXML.Send
strResponse = objXML.responsetext
MsgBox strResponse
End Sub
精彩评论