2017-02-12 187 views
-1

我想將以下PHP代碼轉換爲JavaScript。將PHP代碼轉換爲JavaScript

<body> 
<?php 

$opts = array('http'=>array('method'=>"GET", 
    'header'=>"User-agent: CUSTOM_USER_AGENT\r\n")); 

$context = stream_context_create($opts); 

$file = file_get_contents("http://example.com/file1", false, $context); 
    ///open http://example.com/file1 with CUSTOM_USER_AGENT 

$pattern = "/^Location:\s*(.*)$/i"; 

$location_headers = preg_grep($pattern, $http_response_header); 
    //find url where this redirects to 

echo $location_headers 
?> 
</body> 
+1

歡迎來到SO。請訪問[幫助],看看有什麼和如何問。在這種情況下,請添加努力,至少預期的行爲和缺少分號 – mplungjan

+0

嘗試添加一些更多的描述您的問題..! – Seeker

+0

那就是所有我只是想將此PHP代碼轉換爲JavaScript。 –

回答

0

,除非你是的node.js

除非CORS啓用

JavaScript不能從另一個起源訪問文件不能在JavaScript重寫這個。

AJAX也無法更改用戶代理:

JQuery Ajax Request: Change User-Agent

最後什麼文件,你在看什麼?它看起來像你正在尋找一個位置標題聲明 - 這甚至不會在Ajax結果中可見