Greasemonkey的AJAX请求不发送数据

Greasemonkey的AJAX请求不发送数据

本文介绍了Greasemonkey的AJAX请求不发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解雇了 GET 要求的Greasemonkey的 GM_xmlhtt prequest()

I am firing a GET request with Greasemonkey's GM_xmlhttpRequest():

$(".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);
      }
    });


这里是服务器code ytube.php


and here is the server code ytube.php:

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

$ _ REQUEST =>命令返回到Word preSS一些数据。 $ _ GET =>返回一个空数组。

$_REQUEST => returns some data related to WordPress.$_GET => returns a blank array.

我无法弄清楚什么是错的。我甚至尝试了 POST 方法了。

I can't figure out what's wrong. I even tried the POST method too.

推荐答案

数据参数只适用于 POST 的方法。如果你想用 GET 请求发送数据时,其附加到网址:

The data parameter only works for POST methods. If you wish to send data with a GET request, append it to the URL:

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 ,发送数据的各种原因。要做到这一点,你需要指定的编码:


But it's better to send data via POST, for a variety of reasons. To do that, you need to specify an encoding:

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:



If you are going to send a lot of data, or complex data, use 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将访问它像这样:


Your PHP would access it like so:

$jsonData   = json_decode($HTTP_RAW_POST_DATA);


更新:
的Greasemonkey和Tampermonkey现在要求你设置 @grant GM_xmlhtt prequest 在元数据块。一定要做到这一点。


Update:
Greasemonkey and Tampermonkey now require that you set @grant GM_xmlhttpRequest in the metadata block. Be sure to do that.

这篇关于Greasemonkey的AJAX请求不发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:57