BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / www-technology / #27556同步于 2014/9/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖

JS用POST请求处理JSON数据

heamon7
2014/9/16镜像同步12 回复
请大家指导一下,想用JS的POST请求处理JSON数据,下面这样写,没有response啊?? 求指导。。。[ema23] <html> <head> <script src="https://cn.avoscloud.com/scripts/lib/av-0.4.1.min.js"> <script type="text/javascript"> var request = new XMLHttpRequest(); var url = "https://42matters.com/api/1/apps/query.json?access_token=37e0dc8d667fb1e1f201a51d03547bcbe618509f"; request.open("POST", url, true); request.setRequestHeader("Content-type", "application/json"); request.onreadystatechange = function () { if (request.readyState == 4 && request.status == 200) { console.log(request.getResposeHeader("Content-type")); alert(request.responseText); } }; var parameters = JSON.stringify({ "query": { "name": "Most Popular Apps", "platform": "android", "query_params": { "sort": "number_ratings", "from": 0, "num": 10, "sort_order": "desc" } } }); request.send(parameters); </script> </head> <body> <h1>heamon7 Great !</h1> </body> </html>
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
Chon机器人#1 · 2014/9/17
request.responseText 这不是response?
liu907030机器人#2 · 2014/9/17
原生js写Ajax是够累的,项目能用jquery么 而且你这个好像是跨域post请求?那就没这么简单了
binux机器人#3 · 2014/9/17
开了 Access-Control-Allow-Origin 是没问题的 你确定API返回是200吗 【 在 liu907030 的大作中提到: 】 : 原生js写Ajax是够累的,项目能用jquery么 : 而且你这个好像是跨域post请求?那就没这么简单了
liu907030机器人#4 · 2014/9/17
<!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax post</title> </head> <body> <script> var request = new XMLHttpRequest(); var url = "https://42matters.com/api/1/apps/query.json?access_token=37e0dc8d667fb1e1f201a51d03547bcbe618509f"; request.open("POST", url, true); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.onreadystatechange = function () { if (request.readyState == 4 && request.status == 200) { console.log(request.responseText); } }; var parameters = JSON.stringify({ "query": { "name": "Most Popular Apps", "platform": "android", "query_params": { "sort": "number_ratings", "from": 0, "num": 10, "sort_order": "desc" } } }); request.send(parameters); </script> </body> </html>
liu907030机器人#5 · 2014/9/17
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ajax post需要这样设置以模仿表单提交来达到发送post请求的目的
heamon7机器人#6 · 2014/9/17
【 在 liu907030 的大作中提到: 】 : [code=xml] : <!doctype html> : <html> : ................... 啊啊啊,爱死你啦,谢谢哦!![ema20]
heamon7机器人#7 · 2014/9/17
【 在 liu907030 的大作中提到: 】 : request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); : ajax post需要这样设置以模仿表单提交来达到发送post请求的目的 大神能写Node.js吗,又遇到bug了[ema13]
liu907030机器人#8 · 2014/9/17
【 在 heamon7 的大作中提到: 】 : 大神能写Node.js吗,又遇到bug了 我也正在学习呢,说说你问题一起讨论讨论呗
heamon7机器人#9 · 2014/9/17
【 在 liu907030 的大作中提到: 】 : 我也正在学习呢,说说你问题一起讨论讨论呗 就是在nodejs里怎样用request包来post一个JSON数据。 学长研二吗??