2013-12-16 2 views
0

JS 및 PHP를 사용하여 간단한 API를 구축하는 데 시간이 많이 걸립니다. 우선 스크립트 튜토리얼 Javascript cross-domain api for your website에서 예제/솔루션을 활용했습니다. 이 코드는 표준 브라우저에서 예상대로 작동합니다. 그러나, IE7에 붙어있어 예제가 작동하지 않습니다.크로스 도메인 자바 스크립트 <-> php

지혜의 모든 단어 (인터넷 익스플로러 7을 사용하지 않는 다른 단어)? 사전에

감사합니다, 케이트

- 나는 (이 매우 더러운 테스트 버전) 다음과 같이 결국 epascarello 제안 사항에 따라

: schock.net에서

클라이언트 측 예 : www.geekality.net에서

<html> 
<script> 
    function jsonpCallback(data){ 
     alert(data); 
    } 

    // Create a new script element 
    var script_element = document.createElement('script'); 

    // Set its source to the JSONP API 
    script_element.src = 'http://myserver.com/api.php?callback=jsonpCallback'; 

    // Stick the script element in the page <head> 
    document.getElementsByTagName('head')[0].appendChild(script_element); 
</script> 
</html> 

서버 측 예 :

<?php 
header('content-type: application/json; charset=utf-8'); 

function is_valid_callback($subject) 
{ 
    $identifier_syntax = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u'; 

    $reserved_words = array('break', 'do', 'instanceof', 'typeof', 'case', 
     'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 
     'for', 'switch', 'while', 'debugger', 'function', 'this', 'with', 
     'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 
     'extends', 'super', 'const', 'export', 'import', 'implements', 'let', 
     'private', 'public', 'yield', 'interface', 'package', 'protected', 
     'static', 'null', 'true', 'false'); 

    return preg_match($identifier_syntax, $subject) && !in_array(strtolower($subject), $reserved_words); 

} 

$data = array(1,2,3,4,5,6,7,8,9); 
$json = json_encode($data); 

# Verify JSON callback and respond 
if (is_valid_callback($_GET['callback'])) 
    exit("{$_GET['callback']}($json)"); 

# Handle a bad request 
header('status: 400 Bad Request', true, 400); 

?> 
+0

어떤 오류가 발생하고 있습니까? "작동하지 않는다"는 것보다 더 구체적인 것을 필요로한다. – Pitchinnate

+0

"이 코드는 표준 브라우저에서 예상대로 작동하지만 IE7에서 멈추었습니다."--- 너무 익숙한 것 같습니다. :) – TheCarver

답변

2

대답은 CORS를 지원하지 않기 때문에 IE7에서하고있는 일을 할 수 없다는 것입니다. 해당 브라우저를 지원하려면 JSONP을 사용해야합니다.

관련 문제