How to retrieve GET variable from ajax response in jquery [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get URL parameter with jQuery
I have a form that posts data over ajax. The response comes back in the form of GET variables in the URL. For example if there was a failure of writing the form data the return address would be: http://example.com/?error=1
How would I check for this using jquery. Right now when I do a console.log on the msg variable I just get the html output of example.com (which I guess makes sense). I need the GET variables though. How would I achieve this?
$('#wp_email_capture').submit(function(e){
var email = $('#wp-email-capture-email').val();
$.ajax({
type: "GET",
url: "/",
data: "wp_capture_action=1&wp-email-capture-email=" + email
}).done(function( msg ) {
console.log(msg)
});
e.preventDefault();
});

AJAX by default will return the content from the request. What you want are the headers.
If you are being given a return address of http://example.com?error=1, then it means this is being returned as a redirect header.
To get the header information from an AJAX request, that has been answered here:
jQuery and AJAX response header

Related

Reload current page with GET after jQuery ajax POST [duplicate]

This question already has answers here:
JavaScript to reload the page as GET request
(3 answers)
Closed 2 years ago.
I have this weird issue here where I have the following ajax call inside a file called index.php:
$.ajax({
method:'post',
data:
{
action: 'del_comment',
comment_id: comment_id,
},
success: function (data) {
location.reload(true);
}
});
The PHP portion of the page which intercepts the ajax request is the following:
if($_POST['action'] == 'del_comment') {
// Do some processing
die;
}
The issue is that the ajax is executed but when the page is reloaded, the request is a POST instead of a GET. I was under the impression that the following line in the success of my ajax call:
location.reload(true);
Should force a GET request, but it doesn't work. How can I reload the same page with a GET request after the ajax call?
User window.location instead of "location.reload(true);" with the get parameters that you want, you have to create custom URL with parameters and you can use it like (window.location = 'YOUR URL?GET PARAMETER=VALUE';)

JavaScript - PHP HttpRequest Security Concern [duplicate]

This question already has answers here:
Users can change html code to delete what they want
(2 answers)
HTML hidden input shouldn't be editable
(6 answers)
Prevent URL editing by users to restrict access to server's resources
(1 answer)
Input form with hidden field how to secure it
(4 answers)
How do I prevent others from sending their own data to my php page?
(5 answers)
Closed 4 years ago.
I am creating an application using JavaScript and PHP.
I post my data to a PHP file and then the PHP file insert my data into the MySQL database.
All good but I have some doubts about security because my dom can be easily manipulated using the browser.
For example my code like below
var req = {
method: 'POST',
url: 'phpfile/send_message.php',
headers: {
'Content-Type': undefined
},
data: {
"UserId": UserId,
"Message": Message,
...
..
}
};
$http(req).then(function successCallback(response) {
//Do Something
}, function errorCallback(response) {
//Do Something
});
anyone can put a breakpoint in this code and easily can change the UserId, therefore, the message sends to another user.
Ok, I know I can minify my Script but I think this is not enough. If someone wants to change data, he can find the code easily.
Is there any way to prevent this?
Any information you can provide me would be greatly appreciated.

how to put variable from javascript to php variable? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
This is my codes using script, its a part of my codes, how can i put the var x into my VALUES in php, in the specified 'NULL' value?
enter code here
var x = document.getElementById("name_stop");
<?php require_once('dbconnect.php'); $result2=mysql_query("INSERT INTO log (status, code)VALUES('login','NULL')"); ?>
You can't move JavaScript variables to PHP unless you refresh the page because while JavaScript is client-side, PHP is server-side. When the page is loaded, PHP's job is done.
You should have a look on how to use ajax.
Moreover, for security of your apps, always check the data which are coming from client-side.
You are looking for AJAX. You will have to send a AJAX request to your phpscript.
The easiest way to send an AJAX request is using the jQuery library.
$.ajax({
type: "POST",
url: "insert.php",
data: { name: x }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});

html form submission via an ajax request? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery AJAX submit form
I have a form on a page A, and instead of me posting to a new page, is there a way that i can do onSubmit ajax request it instead? I want to make the page to be able to load in the result into a div.
I know how to write the script for the call sort of, but i wasn't sure if i had to hand write it or it if was possible to take all the form information and send it without having to code it all into the attributes. i was thinking I could just do some sort of thing like: onClick, post form but return results into a div.
Thoughts? Ideas?
Try using JQuery's serialize (API ref here)
EDIT: something like this:
$("input.submit").click( function() {
$.ajax( {
type: "POST",
url: $("form").attr( 'action' ),
data: $("form").serialize(),
success: function( response ) {
//Code to process the response
}
});
return false;
} );
$.ajax({
url: 'mypage.html',
success: function(data){
$('.result').html(data);
},
error: function(){
alert('failure');
}
});
Then you have a div:
<div class="result"> </div>
This will automatically populate with the result of your ajax post.
http://api.jquery.com/jQuery.post/
Also see TONS of related questions: Jquery checking success of ajax post

which method of jquery ajax should I use?

I want to send a very long string to a servlet. Could be more than 10,000 characters.
I want to know which jquery ajax/ or any way to send it to the servlet.
I have used $.post and faced characters limit problems.
sending long strings in jquery post
using $.post has character limits?
Did you send the string as part of the URL (GET) or did you send the string as part of the POST body?
Use this to send it as POST:
$.post(url, {longString: veryLongString}, function(){});
$.post is just an alias for $.ajax (with the method param preset) => it won't make any difference which of these methods you will use.
In case there is any doubt left in your mind here:
function ajaxTest () {
var longstr = "qwertyuiopasdfghjklzxcvbnm";
while (true) {
longstr += longstr;
if (longstr.length > 100000) {
break;
}
}
console.log(longstr.length);
$.ajax({
url: '/ajax',
type: 'post',
data: longstr,
processData: false,
success: function (reply) {
console.log(reply);
}
});
}
I set the server up to reply with "ok" + the length of the post data received. The first console log reports "106496", the second one "ok 106496".
There is no client side limit (eg, imposed by jquery) to how much data you can send via post.
As far as I know there are no character limitations on a POST request. GET has limitations, but I cant recall any on POST operations.
In the above links (the ones you made reference to):
$.post(
"TestServletAsh?xml="+str,
function(data) {
alert("mission successfull"); //nothing to do with it or data here in this SO question
}
);
sends the variable 'str' as a get parameter.
The way to send data to a POST request using jquery is
$.post(url, {data:'whatever you want blah blah blah'}, function(data){});
there is no limit in post, check your js code, you have some mistake or using the get method instead of post

Categories

Resources