AJAX 请求返回“无效参数”,请求无需代理即可工作
我正在尝试执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您提供的信息,我猜测服务器期望您的标头或 cookie 数据中包含更多数据。在 Ajax 页面上抓取数据时,最好使用 Curl 库而不是 file_get_contents()。
当您直接在浏览器中输入请求时,请使用 firefox firebug 扩展中的 Net 选项来准确查看标头中传递的内容。复制这些标头并将其设置在 CURL 中。如果这不起作用,则可能是 cookie 问题。让 CURL 访问原始页面,存储 cookie 并将其用于第二次请求。
例如:
我忘记了:您可以使用 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:
I forgot: You can use the CURLOPT_PROXY option in CURL to set the proxy