AJAX 请求返回“无效参数”,请求无需代理即可工作

发布于 2024-12-29 11:38:53 字数 1282 浏览 0 评论 0原文

我正在尝试执行 ajax 请求以从服务器读取 xml。我正在使用代理来执行此操作。如果我直接在浏览器中输入请求,它会返回正确的 XML。当我使用代理时,它返回“无效参数”。有什么想法吗?

Proxy.PHP

<?php
$c = file_get_contents((urldecode($_REQUEST['u'])));
$content_type = 'Content-Type: text/plain';
for ($i = 0; $i < count($http_response_header); $i++) {
    if (preg_match('/content-type/i',$http_response_header[$i])) {
        $content_type = $http_response_header[$i];
    }
}
if ($c) {
    header($content_type);
    echo $c;
}
else {
    header("content-type: text/plain");
    echo 'There was an error satisfying this request.';
}
?>

请求:

$.ajax({
        type: "GET",
        url:  'proxy.php?u=' + 'http://192.168.100.147:8080/thredds/sos/cfpoint/timeSeriesProfile-Ragged-MultipeStations-H.5.3/timeSeriesProfile-Ragged-MultipeStations-H.5.3.nc?request=GetObservation&service=SOS&version=1.0.0&responseFormat=text%2Fxml%3B%20subtype%3D%22om%2F1.0.0%22&offering=urn:tds:station.sos:Station1&procedure=urn:tds:station.sos:Station1&observedproperty=temperature&eventTime=1990-01-01T00:00:00Z/1990-01-01T00:00:00Z',
        dataType: "xml",
        success: parseSOSGetObs,
        error: function () {alert("AJAX ERROR for " + capRequest );}
});

谢谢!

I am trying to do an ajax request to read xml from a server. I am using a proxy to do so. If i enter the request directly in the browser, it returns the correct XML. When I use my proxy, it returns "Invalid Parameter." Any idea?

Proxy.PHP

<?php
$c = file_get_contents((urldecode($_REQUEST['u'])));
$content_type = 'Content-Type: text/plain';
for ($i = 0; $i < count($http_response_header); $i++) {
    if (preg_match('/content-type/i',$http_response_header[$i])) {
        $content_type = $http_response_header[$i];
    }
}
if ($c) {
    header($content_type);
    echo $c;
}
else {
    header("content-type: text/plain");
    echo 'There was an error satisfying this request.';
}
?>

request:

$.ajax({
        type: "GET",
        url:  'proxy.php?u=' + 'http://192.168.100.147:8080/thredds/sos/cfpoint/timeSeriesProfile-Ragged-MultipeStations-H.5.3/timeSeriesProfile-Ragged-MultipeStations-H.5.3.nc?request=GetObservation&service=SOS&version=1.0.0&responseFormat=text%2Fxml%3B%20subtype%3D%22om%2F1.0.0%22&offering=urn:tds:station.sos:Station1&procedure=urn:tds:station.sos:Station1&observedproperty=temperature&eventTime=1990-01-01T00:00:00Z/1990-01-01T00:00:00Z',
        dataType: "xml",
        success: parseSOSGetObs,
        error: function () {alert("AJAX ERROR for " + capRequest );}
});

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-01-05 11:38:53

根据您提供的信息,我猜测服务器期望您的标头或 cookie 数据中包含更多数据。在 Ajax 页面上抓取数据时,最好使用 Curl 库而不是 file_get_contents()。
当您直接在浏览器中输入请求时,请使用 firefox firebug 扩展中的 Net 选项来准确查看标头中传递的内容。复制这些标头并将其设置在 CURL 中。如果这不起作用,则可能是 cookie 问题。让 CURL 访问原始页面,存储 cookie 并将其用于第二次请求。

例如:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2");
$junk = curl_exec($ch);
curl_close($ch);


$headers = array("X-Prototype-Version: 1.6.0", "X-Requested-With: XMLHttpRequest");
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/setsomething.sync");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, "http://originalsite.com/");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$page = curl_exec($ch);
curl_close($ch);
?>

我忘记了:您可以使用 CURL 中的 CURLOPT_PROXY 选项来设置代理

From the information you've given, I would guess that the server is expecting more data in your header or cookie data. When scraping data on Ajax pages it is better to use the Curl Library instead of file_get_contents().
When you enter the request directly into the browser, use the Net option in firefox firebug extension to see exactly what is being passed in the headers. Copy those headers and set them in CURL. If that doesn't work, it could be a cookie issue. Have CURL visit the original page, store the cookies and use them for the second request..

Ex:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2");
$junk = curl_exec($ch);
curl_close($ch);


$headers = array("X-Prototype-Version: 1.6.0", "X-Requested-With: XMLHttpRequest");
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/COOKIE");
curl_setopt($ch, CURLOPT_URL, "http://originalsite.com/setsomething.sync");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, "http://originalsite.com/");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0a2) Gecko/20111101 Firefox/9.0a2");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$page = curl_exec($ch);
curl_close($ch);
?>

I forgot: You can use the CURLOPT_PROXY option in CURL to set the proxy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文