研究了好几天也没研究出用AJAX跨域访问微信企业号接口,而用ASP就能很容易地跨域访问。
核心代码如下:
<script language="javascript" runat="server">
function evalJsonStr(str){return eval("(" + str + ")");}
</script>
<%
'自定义可以跨域请求的函数jsonObjByUrl,
'为了让ASP能解析JSON字符串,所以要用到上面用JS自定义的函数
'url为要跨域请求的网址
'method为GET或POST提交
'data为要与url同时提交数据,书写格式如data="{""msgtype"":""text"",""text"":{""content"":"""&msgStr&"""}}"
function jsonObjByUrl(url,method,data)
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.open method,url,false
objXML.send(data)
set jsonObjByUrl=evalJsonStr(objXML.responseText)
set objXML=nothing
end function
corpid="换成你们的企业的corpid"
corpsecret="换成你们的企业的corpsecret"
url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" & corpid & "&corpsecret=" & corpsecret
method="GET":dataStr=""
set obj=jsonObjByUrl(url,method,dataStr)
ACCESS_TOKEN=obj.access_token
url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" & ACCESS_TOKEN
msgStr=“要发送的文字内容”
dataStr="{""touser"":""@all"",""msgtype"":""text"",""agentid"":""3"",""text"":{""content"":"""&msgStr&"""},""safe"":""0""}"
method="POST":dataStr=""
set obj=jsonObjByUrl(url,method,dataStr)
response.Write(obj.errmsg)
set obj=nothing
%>