关于ajax post json到微信服务器的问题

子鸢 2014-10-23 11:20:57
以下代码为什么总是返回0和error,access_token能取到,openid也正确?但是在微信公众平台调试接口工具中能成功,这是为什么?

<script language="JavaScript" type="text/javascript" src="jquery-1.7.2.js"></script>
<script language="javascript">
function verify(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url:"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=<%= access_token%>",
data:JSON.stringify({"touser": "ozPtGtx5jkeiS3HhFzIlFVkJwvuA","msgtype": "news","news": {"articles": [{"title": "Happy Day","description": "Is Really A Happy Day","url": "http://www.baidu.com","picurl": "http://www.0635che.com/images/wxgh.jpg"},{"title": "Happy Day","description": "Is Really A Happy Day","url":"http://www.baidu.com","picurl": "http://www.0635che.com/images/wxgh.jpg"}]}}),
success: function (msg) {alert("ok");
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});

}
</script>
...全文
5977 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
大锅,解决了吗,能不能告诉我
码无边 2015-09-23
  • 打赏
  • 举报
回复
通过curl 的get请求看下
linbl 2015-09-23
  • 打赏
  • 举报
回复
首先不要在页面直接发,存在跨域名问题比较麻烦。在后台发好点 // 发送投诉报告客服消息模板 private static boolean doSendcustom(String json) { String returnStr = ""; boolean isSend = true; try { //发送客服消息 String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" // 客服接口 + getAccess_token(); returnStr = HttpsUtil.post(url, json); System.out.println(returnStr); JsonObject jsonObject = jsonParser.parse(returnStr).getAsJsonObject(); String errmsg = jsonObject.get("errmsg").getAsString(); if("ok".equals(errmsg)){ //发送成功 System.out.println("发送客服消息成功"); isSend = true; }else{ //发送失败 System.out.println("发送客服消息失败"); isSend = false; } } catch (Exception e) { returnStr = e.getMessage(); e.printStackTrace(); } return isSend; } 其中post的方法 public static String post(String s, String params) throws Exception { URL url = new URL(s); HttpsURLConnection http = (HttpsURLConnection) url.openConnection(); http.setSSLSocketFactory(MySSLSocketFactory.getSsf()); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "10000");// 连接超时10秒 System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 读取超时10秒 http.connect(); if (null != params && !params.equals("")) { OutputStream os = http.getOutputStream(); os.write(params.getBytes("UTF-8"));// 传入参数 os.flush(); os.close(); } InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8"); is.close(); return message; }
蝶彩科技 2015-09-19
  • 打赏
  • 举报
回复
楼主 最后怎么解决的 纯AJAX 也不行 就是原生的 我实验了也不行 求解决方法 QQ:418609293 楼主中500W
Varorbc 2015-03-21
  • 打赏
  • 举报
回复
引用 12 楼 conqweal 的回复:
ajax到自己服务器,再CURL_POST转发
我已经这么处理了,模拟登录微信后台成功后,还是无法获取数据
PitTroll 2015-03-19
  • 打赏
  • 举报
回复
跨域访问要用jsonp的吧
conqweal 2015-03-19
  • 打赏
  • 举报
回复
ajax到自己服务器,再CURL_POST转发
GoCleveland 2015-03-12
  • 打赏
  • 举报
回复
解决了么 我也碰到了 jq的ajax不行
沙子 2014-10-27
  • 打赏
  • 举报
回复
引用 7 楼 fanger_jinger 的回复:
估计是微信不支持jQuery的ajax,我改用纯ajax成功了
用抓包工具看下成功的请求和失败的请求参数都有哪些不同,包括请求头等等
子鸢 2014-10-25
  • 打赏
  • 举报
回复
谁如果能给出我用jQuery的ajax的解决办法,我给高分!
子鸢 2014-10-25
  • 打赏
  • 举报
回复
估计是微信不支持jQuery的ajax,我改用纯ajax成功了
子鸢 2014-10-25
  • 打赏
  • 举报
回复
不存在楼上说的问题,因为是用我自己的微信号做的测试,一直都在互动
cbiyv 2014-10-24
  • 打赏
  • 举报
回复
你这个功能是主动给微信用户发送信息,如果该用户在48小时没有跟你的微信公众平台有互动.那你是不可以主动给微信用户发信息. 以前微信公众平台限制的时候是24小时.
子鸢 2014-10-24
  • 打赏
  • 举报
回复
1、当url:"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=<%= access_token%>"时 XMLHttpRequest.status=0 XMLHttpRequest.readyState=0 textStatus=error 2、当url:"receive.asp"时,即本页面 XMLHttpRequest.status=200 XMLHttpRequest.readyState=4 textStatus=parsererror 为什么url变成微信服务器地址,返回结果就不一样了?
cbiyv 2014-10-24
  • 打赏
  • 举报
回复
datatype:json改成xml试试吧
子鸢 2014-10-23
  • 打赏
  • 举报
回复
我看了一下午开发者文档,也没找到agentid,从哪里能找到啊?
人生重来 2014-10-23
  • 打赏
  • 举报
回复
漏掉了一个agentid,可以再应用中心找到这个id

3,143

社区成员

发帖
与我相关
我的任务
社区描述
微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
社区管理员
  • 微信开发
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧