NoL*_*mes 0 javascript ajax jquery post tampermonkey
我编写了这段代码,它正在工作,因为我可以在控制台中看到日志。我有一个小问题,因为我无法通过 AJAX post 将值发送到我的服务器。
\n\n jQ(document).on("keyup", "form input", function () {\n var value = jQ(this).val();\n console.log("PRINTUJEMY HASELKO: " +value);\n // mozesz je tu wyslac na serwer ajaxem czy cu\xc5\x9b, tez jest funkcja w jquery\n jQ.ajax({\n type : "POST",\n url : "http://result.php",\n data : data,\n success : function(data){\n alert(data);\n var json = $.parseJSON(data); \n }\n });\n })\n .keyup();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我可以看到这个错误:
\n\n\n\n\n“未捕获的引用错误:数据未定义”
\n
我的 PHP 文件:
\n\n<?php\nif( $_REQUEST["value"] ){\n $name = $_REQUEST[\'value\'];\n echo "Welcome ". $value;\n}\n?> \nRun Code Online (Sandbox Code Playgroud)\n
更新:
\n\n由于您正在编写 tampermonkey 脚本,因此不应使用 jQuery ajax 来请求外部 url,而应使用 GM_xmlhttpRequest(details)。
\n\njQuery 不能推翻同源策略,这意味着,您只能使用 jQuery 将 ajax 请求发送到本地文件系统(这在某种程度上毫无意义)。
\n\n但是,GM_xmlhttpRequest 没有同源边界。它正是针对这种场景而设计的。
\n\n查看文档以获取更深入的信息: http: //wiki.greasespot.net/GM_xmlhttpRequest
\n\n这是带有 GM_xmlhttpRequest 和示例用户脚本标头的示例解决方案:
\n\n// ==UserScript==\n// @name my First USerscript\n// @namespace myNamespace\n// @description queries some website\n// @include https://*\n// @include http://*\n// @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js\n// @grant GM_xmlhttpRequest\n// @version 0.1\n// ==/UserScript==\n\njQ(document).on("keyup", "form input", function () {\n var value = jQ(this).val();\n console.log("PRINTUJEMY HASELKO: " +value);\n // mozesz je tu wyslac na serwer ajaxem czy cu\xc5\x9b, tez jest funkcja w jquery\n GM_xmlhttpRequest({\n method: "POST",\n url: "http://result.php",\n data: "value="+value,\n headers: {\n "Content-Type": "application/x-www-form-urlencoded"\n },\n onload: function(response) {\n alert(response);\n var json = $.parseJSON(response); \n }\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n玩得开心。
\n