javascript - 使用curl进行ajax查询

标签 javascript ajax curl http-headers

我想从一个网站上抓取信息来编译有关阿根廷机场的信息。该网站是 http://www.anac.gov.ar/anac/web/index.php/2/310/informacion-aeronautica/notam 。我试图让curl 获取通过ajax 查询访问的页面中的数据

<script type="text/javascript">
    var lugares = document.getElementById("locations");
    $(lugares).change(
            function() {
                $('.loading').show();
                $('.ajaxbody').hide();
                $.ajax(
                        {
                            url: "/notam/pib",
                            type: "POST",
                            dataType: "html",
                            data: {indicador: $(lugares).val()}
                        }).done(function(html) {
                    tabla = document.getElementById('pib');
                    $(tabla).find('table').remove();
                    $(tabla).append(html);
                    $('.loading').hide();
                    $('.ajaxbody').fadeIn('slow');
                });
            });
</script>

这就是我使用curl的方式curl -X POST -H“dataType:html”-d'/notam/p ib'-H“指标=TRE”http://www.anac.gov.ar/anac/web/index.php/2/310/informacio n-航空/notam

但是curl想要解析-H“indicador=TRE”,因为它是一个url,但它是在POST中发送的数据。

我看到页面使用wireshark发出的真实POST,这就是它看到的内容

POST /notam/pib HTTP/1.1

Host: 186.153.175.229

User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0

Accept: text/html, */*; q=0.01

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

X-Requested-With: XMLHttpRequest

Referer: http://186.153.175.229/portal/notam

Content-Length: 13

Cookie: PHPSESSID=k1daatvv9882drooofid7cvvc6

Connection: keep-alive

Pragma: no-cache

Cache-Control: no-cache



indicador=TREHTTP/1.1 200 OK

Date: Tue, 18 Feb 2014 23:30:41 GMT

Server: Apache/2.2.22 (Ubuntu)

X-Powered-By: Fat-Free Framework

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Pragma: no-cache

Access-Control-Allow-Origin: http://geo.anac.gov.ar

Vary: Accept-Encoding

Content-Encoding: gzip

Content-Length: 1512

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html; charset=UTF-8



...........Y.r.H...O.gm.j."..1DsE.Dj.<P....PF........>..=......QI61...G........f.W.......7..s.x..I.......Q/w.
....P...Zx.^..\.......9.zq....^.N.p.E.!........3.y.On..[.hL.....Q<...7...%.,|h......S....#.D...w.K.._.!...l..
..Q?...]#....g....x}..a.ir..
.E.Hx.eo;.._...4..j..Iqj|.o...[F...|.....:B......Oe`....!0"...,..M..M.O....k.h2,..t...VLPZ-.V...E....C..!.*.v..E..].....s.....-...&.......j2;...G....q.68...2F..G....m..S.[dp$.
..e..Di.. ....AxK.)...S...y.=..7....>?...yA.t.F.@.l.tD..?.ME5l.Z8..8...@.-.....f[8.f..=<4...t.pR(>n.1.............."..4.W.I.2...1UY.U..M.L.....G... et....].5.E
.q...;q.6..?.{... .i.+@*......X.. ...A..6.%0....Z...2.W..J....Mi...n-E......A.G.PM.........*m.4N.......N....p*..............R.v#..
P....)<.c...\..".e...(.3...P.3..%vK....6N..8......9....DD,U %..d...=2.Z.......f..0...........R......\...h....H/F6....M3..m..&6..
..n.qc1...).3..\.+.r.4b.6j.3h...R&..+.D..1kW@DR,C..i.-.....+..Hu...D...."...y.v.6.8.X..R.J.A.B...h.{...H.......y.P..*.t.$z.!....t>....Sa?.....5.t.t..&.r.K5Zx:aI$/#.....N......4.D.k.....3...?x!8..&....|...v....hY
X..{Q.m...d.X.......R...OO"G..U........!.......3?.
...o......QT.NI..%.k. .^...[.*.'....,>.8..s.O....<.vd...O&..>.n.f..E..Q..]..9.&w.....5..`4.e..0......Q8d.O..p.I..8.j.r..?...$
a....^.5...&.|.....4......&..z.&I&2.c]L..(.........~....8..a.xc.....7t...O.z.............m....\.~{............}I..i?.M.H.a....[/m]..:.P....B.y........;...m5..|..ZEau.....hBC}0..D..a....... .....Y.....^...6.
...../m...V=Ob.D./c..)..G...hH..h...1.$].@k........{......8...?..... ..

谢谢。

最佳答案

你的curl命令令人困惑。特别是网址。所以我在命令中使用了一个虚拟 URL。

curl -X POST -H "dataType: html" -d "/notam/p ib" -H "indicador: TRUE" http://url/

我已将您的单引号替换为双引号,还调整了 -H 之前的空格。

关于javascript - 使用curl进行ajax查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867837/

相关文章:

javascript - 在页面模板中使用 Angular ui-router State

javascript - ngroute 后指令加载不正确

http - 如何在不使用 -F 的情况下重写此 CURL 多部分/表单数据请求?

java - Java 中的 Curl oAuth 等效项

javascript - jQuery JSON 发布 : Invalid object passed in, ':' 或 '}' 预期

javascript - Three.js color.setHex 在组中的所有网格上

javascript - 尝试在函数之间传递 jQuery 移动范围 slider 值不起作用

javascript - 对 "refresh all"事件使用相同的 ajax 数据(想要防止多次调用)

asp.net - 在 RadAjax 回调后执行客户端 Javascript

PHP curl - 发布 asp.net viewstate 值