Pass a nested object to POST request [duplicate] - javascript

This question already has answers here:
Jquery Ajax Posting JSON to webservice
(5 answers)
Closed 8 years ago.
How can I send a nested object via POST request?
var name = "test",
path = "?diffCurr%5B%5D=FALSE&diffProv%5B%5D=FALSE",
data = {
datatype:"report",
"value":{
"name":name,
"query":path
}
};
$.ajax({
type:"POST",
url: "resources/savedata.html",
data: data,
success: function(data){
...
},
complete: function(){
...
}
})
When I check in the chrome, in the network tab under "form data", I see this:
datatype:report
value[name]:test
value[query]:?diffCurr%5B%5D=FALSE&diffProv%5B%5D=FALSE
Basically, I was expecting $_POST["value"] to contain an object with name and query.

Basically, I was expecting $_POST["value"] to contain an object with name and query.
It does.
You are just looking at the raw data which is encoded in application/x-www-form-urlencoded with PHP's square bracket syntax for complex objects.
When PHP deserializes it to populate $_POST, it will do so in the form you expect.

One of the easiest way is serializing in a Json string your nested object
$.ajax({
type:"POST",
url: "resources/savedata.html",
data: JSON.stringify(data),
success: function(data){
...
},
complete: function(){
...
}

Related

Receive data with flask and send data with jQuery [duplicate]

This question already has answers here:
How do I get the different parts of a Flask request's url?
(4 answers)
jQuery posting JSON
(3 answers)
How to get POSTed JSON in Flask?
(13 answers)
Closed 4 years ago.
!! THIS IS NOT A DUPLICATE !!
The question was not how to get an URL in Flask, but how to send data with jQuery to Flask!
I try to send and receive data with python, Flask and jQuery
The problem is that I want the full URL of the website and it is impossible to get it with flask because I make 'POST' requests. So with jQuery, I want to send the current URL.
I don't know how to send data (with jQuery) and receive data (with Flask).
Python/Flask code:
#app.route('/invisible', methods = ['POST'])
def dynamic_refresh():
return jsonify({'somedata': 'data'})
HTML/jQuery code:
<script>
$(document).ready(function() {
window.setInterval(function() {
$.ajax({
type : 'POST',
url : '/invisible',
//I tried to send data from here but it didn't worked
})
.done(function(data) {
console.log(data)
console.log(window.location.href)//the url I want to send
//here I use the data received by the server
})
}, 5000);
});
</script>
Its quite simple, enclose data in JSON array which you want to send through POST request and then retrieve any data from Flask endpoint like this;
var url = $('#url').val().trim(); //get your value from HTML here
var params = {
_url: url,
};
var array = JSON.stringify(params); //enclosed it in json array
$.ajax({
type: "POST",
url: "/invisible",
data: array,
dataType: 'json',
success: function(results){
console.log(results)
}
});

Passing JQuery var to PHP using AJAX [duplicate]

This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 7 years ago.
I'm trying to pass 3 variables that look like this
var location = document.location.pathname;
var search = document.location.search;
var referrer = document.referrer;
Into a PHP file that I can eventually use to send emails, I have never used AJAX before but know that you could use it to achieve this.
Can anyone help me out? Thanks in advance.
A simple Ajax POST method can help you here. Here's an example.
$.ajax({
type: "POST",
url: "ajax.php",
data: {location: location, search: search, referrer: referrer},
success: function(response){
//do something
}
})//ajax end
Now in ajax.php, you can receive the values via $_POST.
PHP Receiving(ajax.php).
var_dump($_POST['location']);
You could do like this:
$.ajax({
type: "POST",
data: {'location': location,
'search': search,
'referrer': referrer
},
url: "Here the path to your php-file",
success: function (data) {
Here you could react on any
}
});
In the php file you receive those data by Post and can handle them.

How to display returned json using jquery ajax [duplicate]

This question already has answers here:
JSON returning [object Object] [duplicate]
(3 answers)
Closed 7 years ago.
I have the following jquery code:
$.ajax({
type: "POST",
url: "Ajax/getTableRecord",
data:{ i : id, t: 'mylist'},
dataType: 'json',
success: function(data){
alert(data);
}
When I activate this, the php function getTableRecord, queries a db and ends with:
echo json_encode($array);
I can see the correct returned data in my console. However the alert function just shows the contents of the screenshot. How do I display the returned key value pairs in the alert box instead?
You can do
console.log(data);
or JSON.stringify() like this
JSON.stringify(data);
Use the console
console.log(data);
instead of
alert(data);
you can check how to open the console on each browser here https://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers

How can I sent an array with $.ajax - dataType: json?

Im currently trying to sent a javascript array to my .php file handler and save it to my database.
The request is successful however it seems my array doesn't get POSTED / saved correctly.
In my POST request source it just turns up as: round_items%5B%5D=1
What am I missing?
id = 5;
var roundChallenges = new Array("item1", "item2", "etc");
//Save the data
var url = path.php;
var request = $.ajax({
type: "POST",
url: url,
dataType: 'json',
data: { uid: id, round_items: roundChallenges },
success: function(data)
{....
round_items%5B%5D=1 is correct. That is what it should be sending. That decodes to round_items[]=1, which is how you make arrays in query strings.
When you pass an object to $.ajax, jQuery converts it to a query string, a standard transport format.
In PHP, you don't need json_decode or anything. It will parse it into $_POST for you. $_POST['round_items'] will be an array, and $_POST['uid'] will be your id.

jQuery.post is not sending data to the specified URL

I have a mobile application and I have a lot of data that I am putting in to a JSON object to store in localStorage. I need to get this data to PHP to process it. I have chosen to use jQuery.ajax to send the data as a JSON object to PHP. However, when I run the function, it gives a success message, but does not go to the url specified. I have a lot of PHP experience but this is my first JS intensive project.
Here is my JS code:
function sendToPHP() {
jQuery.ajax({
type: "POST",
url: "email.php",
data: { "json" : ATRdataJSON},
success: function(data){
console.log("Data Sent!");
},
});
};
ATRdataJSON is a JSON object that has several JSON objects nested inside.
The URL may not be pointing where you think it's pointing. Try:
function sendToPHP() {
jQuery.ajax({
type: "POST",
url: "/email.php",
data: { "json" : ATRdataJSON},
success: function(data){
console.log("Data Sent!");
},
});
};
i'm afraid you cannot send the json object without stringifying it, it may be sent but as a string [object] try to check it first then you may make sure of the url is absolute to make sure it goes to the right controller.

Categories