2012-02-22 3 views
4

나는 그리스 몽키의 GM_xmlhttpRequest()GET 요청을 발사하고 => 워드 프레스와 관련된 몇 가지 데이터를 반환Greasemonkey AJAX 요청이 데이터를 보내지 않고 있습니까?

<?php 
    print_r($_REQUEST); 
    print_r($_GET); 
    echo "Hello friends".$_GET['vid']; 
?> 

$_REQUEST : 여기

$(".getReview").click(function(){ 
    var videoId = $(this).parents("li").find("a").attr("href"); 
    alert(videoId); 
    GM_xmlhttpRequest({ 
     method: "GET", 
     url: "http://www.amitpatil.me/demos/ytube.php", 
     data: "username=johndoe&password=xyz123", 
     headers: { 
     "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used. 
     "Accept": "text/xml"   // If not specified, browser defaults will be used. 
     },   
     onload: function(response) { 
      console.log(response); 
     } 
    }); 


을하고하는 것은 서버 코드 ytube.php에게 있습니다. $_GET => 공백 배열을 반환합니다.

무엇이 잘못되었는지 알 수 없습니다. 나는 심지어 POST 방법을 시도했다.

답변

5

data 매개 변수는 POST 방법을 작동합니다.

GM_xmlhttpRequest ({ 
    method: "GET", 
    url: "http://www.amitpatil.me/demos/ytube.php?username=johndoe&password=xyz123", 
    // Use no data: argument with a GET request. 
    ... ... 
}); 

을하지만 여러 가지 이유로, POST를 통해 데이터를 전송하는 것이 좋습니다 : 당신이 GET 요청에 데이터를 전송하고자하는 경우, URL에 추가합니다.

GM_xmlhttpRequest ({ 
    method: "POST", 
    url: "http://www.amitpatil.me/demos/ytube.php", 
    data: "username=johndoe&password=xyz123", 
    headers: { 
     "Content-Type": "application/x-www-form-urlencoded", 
     "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used. 
     "Accept": "text/xml"   // If not specified, browser defaults will be used. 
    }, 
    ... ... 
}); 


당신은 데이터 또는 복잡한 데이터를 많이 보내려고하는 경우에, 사용 JSON :

var ajaxDataObj = { 
    u: username, 
    p: password, 
    vidInfo: [123, "LOLcats Terrorize City!", "Five stars"] 
}; 

var serializedData = JSON.stringify (ajaxDataObj); 

GM_xmlhttpRequest ({ 
    method: "POST", 
    url: "http://www.amitpatil.me/demos/ytube.php", 
    data: serializedData, 
    headers: { 
     "Content-Type": "application/json", 
     "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used. 
     "Accept": "text/xml"   // If not specified, browser defaults will be used. 
    }, 
    ... ... 
}); 

그렇게하려면, 당신은 인코딩을 지정해야합니다

PHP가 다음과 같이 액세스합니다 :


업데이트 :
그리스 몽키와 Tampermonkey 지금 필요로하면 메타 데이터 블록에 set @grant GM_xmlhttpRequest. 그렇게해야합니다.

관련 문제