admin管理员组文章数量:1022670
I wrote this code, and it's working because I can see the logs in console. I have a little problem because i can't send the values to my server via AJAX post.
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
jQ.ajax({
type : "POST",
url : "",
data : data,
success : function(data){
alert(data);
var json = $.parseJSON(data);
}
});
})
.keyup();
}
I can see this error:
"Uncaught ReferenceError: data is not defined"
My PHP file:
<?php
if( $_REQUEST["value"] ){
$name = $_REQUEST['value'];
echo "Wele ". $value;
}
?>
I wrote this code, and it's working because I can see the logs in console. I have a little problem because i can't send the values to my server via AJAX post.
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
jQ.ajax({
type : "POST",
url : "http://result.php",
data : data,
success : function(data){
alert(data);
var json = $.parseJSON(data);
}
});
})
.keyup();
}
I can see this error:
"Uncaught ReferenceError: data is not defined"
My PHP file:
<?php
if( $_REQUEST["value"] ){
$name = $_REQUEST['value'];
echo "Wele ". $value;
}
?>
Share
Improve this question
edited Mar 5, 2015 at 12:55
mondjunge
1,24914 silver badges24 bronze badges
asked Mar 3, 2015 at 13:45
NoLimesNoLimes
812 silver badges12 bronze badges
9
-
data:value
notdata:data
– Sandeep Nayak Commented Mar 3, 2015 at 13:46 - A huge mistake :) I changed it but it`s does not working anyway. – NoLimes Commented Mar 3, 2015 at 14:15
-
Did you check the url ? As far as I know,
http://result.php
is not valid as there's no host in that :) – Arkantos Commented Mar 3, 2015 at 14:38 - I wrote this because i want to hide my website. – NoLimes Commented Mar 3, 2015 at 14:54
- Take a look at my updated answer. – mondjunge Commented Mar 5, 2015 at 13:29
3 Answers
Reset to default 5UPDATE:
Since you are coding a tampermonkey script, you should not use jQuery ajax to request your external url, but GM_xmlhttpRequest( details ).
jQuery cannot overrule the same origin policy, meaning, you could only send ajax requests to the local filesystem with jQuery (what is somehow senseless).
However, GM_xmlhttpRequest has no same-origin boundaries. It is exactly made for this scenario.
Take a look at the documentation for deeper information: http://wiki.greasespot/GM_xmlhttpRequest
This is an example solution with GM_xmlhttpRequest AND example userscript header:
// ==UserScript==
// @name my First USerscript
// @namespace myNamespace
// @description queries some website
// @include https://*
// @include http://*
// @require https://ajax.googleapis./ajax/libs/jquery/1.9.0/jquery.min.js
// @grant GM_xmlhttpRequest
// @version 0.1
// ==/UserScript==
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
GM_xmlhttpRequest({
method: "POST",
url: "http://result.php",
data: "value="+value,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
alert(response);
var json = $.parseJSON(response);
}
});
});
Have Fun.
Your data
property should be:
data: { value : value },
The reference error occurred because your data is stored in value
not data
. You were also trying to send it as raw POST data (without any key), and since your server side is looking for the value
key, you should pass an object as above.
Side note: you won't be able to $.parseJSON()
on your current response data, because the string Wele x
isn't valid JSON, but you should see the alert before that.
jQ.ajax({
type: "POST",
url: "http://result.php",
data: value,
success: function(data){
alert(data);
var json = $.parseJSON(data);
}
});
I wrote this code, and it's working because I can see the logs in console. I have a little problem because i can't send the values to my server via AJAX post.
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
jQ.ajax({
type : "POST",
url : "",
data : data,
success : function(data){
alert(data);
var json = $.parseJSON(data);
}
});
})
.keyup();
}
I can see this error:
"Uncaught ReferenceError: data is not defined"
My PHP file:
<?php
if( $_REQUEST["value"] ){
$name = $_REQUEST['value'];
echo "Wele ". $value;
}
?>
I wrote this code, and it's working because I can see the logs in console. I have a little problem because i can't send the values to my server via AJAX post.
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
jQ.ajax({
type : "POST",
url : "http://result.php",
data : data,
success : function(data){
alert(data);
var json = $.parseJSON(data);
}
});
})
.keyup();
}
I can see this error:
"Uncaught ReferenceError: data is not defined"
My PHP file:
<?php
if( $_REQUEST["value"] ){
$name = $_REQUEST['value'];
echo "Wele ". $value;
}
?>
Share
Improve this question
edited Mar 5, 2015 at 12:55
mondjunge
1,24914 silver badges24 bronze badges
asked Mar 3, 2015 at 13:45
NoLimesNoLimes
812 silver badges12 bronze badges
9
-
data:value
notdata:data
– Sandeep Nayak Commented Mar 3, 2015 at 13:46 - A huge mistake :) I changed it but it`s does not working anyway. – NoLimes Commented Mar 3, 2015 at 14:15
-
Did you check the url ? As far as I know,
http://result.php
is not valid as there's no host in that :) – Arkantos Commented Mar 3, 2015 at 14:38 - I wrote this because i want to hide my website. – NoLimes Commented Mar 3, 2015 at 14:54
- Take a look at my updated answer. – mondjunge Commented Mar 5, 2015 at 13:29
3 Answers
Reset to default 5UPDATE:
Since you are coding a tampermonkey script, you should not use jQuery ajax to request your external url, but GM_xmlhttpRequest( details ).
jQuery cannot overrule the same origin policy, meaning, you could only send ajax requests to the local filesystem with jQuery (what is somehow senseless).
However, GM_xmlhttpRequest has no same-origin boundaries. It is exactly made for this scenario.
Take a look at the documentation for deeper information: http://wiki.greasespot/GM_xmlhttpRequest
This is an example solution with GM_xmlhttpRequest AND example userscript header:
// ==UserScript==
// @name my First USerscript
// @namespace myNamespace
// @description queries some website
// @include https://*
// @include http://*
// @require https://ajax.googleapis./ajax/libs/jquery/1.9.0/jquery.min.js
// @grant GM_xmlhttpRequest
// @version 0.1
// ==/UserScript==
jQ(document).on("keyup", "form input", function () {
var value = jQ(this).val();
console.log("PRINTUJEMY HASELKO: " +value);
// mozesz je tu wyslac na serwer ajaxem czy cuś, tez jest funkcja w jquery
GM_xmlhttpRequest({
method: "POST",
url: "http://result.php",
data: "value="+value,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
alert(response);
var json = $.parseJSON(response);
}
});
});
Have Fun.
Your data
property should be:
data: { value : value },
The reference error occurred because your data is stored in value
not data
. You were also trying to send it as raw POST data (without any key), and since your server side is looking for the value
key, you should pass an object as above.
Side note: you won't be able to $.parseJSON()
on your current response data, because the string Wele x
isn't valid JSON, but you should see the alert before that.
jQ.ajax({
type: "POST",
url: "http://result.php",
data: value,
success: function(data){
alert(data);
var json = $.parseJSON(data);
}
});
本文标签: javascriptJquery Ajax Sending post with values from inputs with tampermonkeyStack Overflow
版权声明:本文标题:javascript - Jquery Ajax. Sending post with values from inputs with tampermonkey - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745507440a2153670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论