We wonder what the colon, :, in data does. We understand that the data is posted to list.php but not exactly how.
function paint(val){
$(".loading").css("display","block");
$.ajax({
type:"POST",
url: "paint.php",
data:{
target:val,
PaintedObjects:PaintedObjects,
},
dataType: 'json',
success: function(data){
Thank you!
The [:] sign - used in object literal
{propertyName: "propertyValue"}
The left side of the : is the object property
The right side of the : is the properties value
in your case
$.ajax({
type:"POST",
url: "paint.php",
"type" is object property and "POST" is properties value.
Thanks
It separates the key name from the value, the same function it operates in the rest of the code you posted (url: etc)
Read more about JSON here
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
The colon separates the key value pairs, like JSON. It doesn't do anything different in 'data' than 'type', 'url', 'dataType', etc. But, the data property can except different data types or structures, unlike some of the other ajax properties.
data accepts PlainObject, String and Array formats. You can have more details at here http://api.jquery.com/Types/#PlainObject
Related
I want to POST 3 parameters to my PHP web service. One of which is an array, so I used JSON.stringify() first and then added it as the parameter. The problem is, PHP isn't receiving the stringify'ed parameter.
My array is created by fetching IDs of each element in an object.
Converting it into a string:
var cid = JSON.stringify(cids);
Output as in the console for cid:
["1","2","3"]
And my $http.post call:
var dataObj = {
wid: wid,
type: type,
cid: cid
};
$http.post("my.php", dataObj);
From what I understand, the array gets converted into a string, which must get POSTed alike other strings, but when I echo back $_POST['cid'], it turns out to be blank; while the wid & type are proper.
From my understanding all post data is converted to JSON anyway. Manually changing your array to JSON and including is just the same as:
$http.post("my.php", {
wid: wid,
type: type,
cid: [1, 2, 3]
}
At the other side of the connection the JSON is converted back to objects(arrays) if possible. PHP doesn't print anything because arrays aren't converted to strings that well. Although you should expect the string 'array'. Try echo'ing $_POST['cid'][0]
EDIT:
As stated in the comments and this post the following code is required to get PHP and AngularJS to play together:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
I have a php file wich searches in a DB, then builds and array with results and it ends and shows the result like this:
{"1":"1","2":"2","15":"15","25":"25"}
That means in position 0 there is no result, position 1 has value 1, and so on. (as far as I /know this result is a valid json output)
In jquery side I have this
$.ajax({
type: 'POST',
url: 'diahoracierre.php',
dataType: 'json',
cache: false,
success: function(result) {
alert(result);
},
});
What I need is to "parse" result to an array and be able to play which the content using index of array, like
alert(result[X]);
on result[1] it shows 1, any other attempt shows undefined and there are at least 4 results.
You have two options either you loop through result and create an array by yourself(a tedious task) or you access elements like below, please note quotes around index is required as here though we are saying index but in reality we are looking for key name.
result["index"]
e.g.
result["5"]
Also I tried your jsFiddle and it worked:
var json = '{"0":"-1","1":"1","2":"2","15":"15","3":"-1","25":"25","4":"-1","5":"-1","6":"-1","7":"-1","8":"-","9":"-1","10":"-1","11":"-1","12":"-1","13":"-1","14":"-1","16":"-1","17":"-1","18":"-1","19":"-1","20":"-1","21":"-1","22":"-1","23":"-1","24":"-1","26":"-1"}';
json = JSON.parse(json);
alert(json["25"]);
Since you've used dataType: 'json'. Then it's no longer require you to parse the json result.
Also note that your parameter is named result in success function.So you can use:
alert(result[1]);
instead of:
alert(myArray[1]);
I am trying to send a javascript array to an external php page, but the only thing the php page is picking up is the fact that I am sending array, not the actual data within the array.
javascript -
var newArray = [1, 2, 3, 4, 5];
$.ajax({
type: 'POST',
url: 'array.php',
data: {'something': newArray},
success: function(){
alert("sent");
}
});
External PHP Page -
<?php
echo($_POST['something']));
?>
I know this question has been asked before, but for some reason, this isn't working for me. I have spent the last couple days trying to figure this out as well. Can someone please point me in the right direction.
current output (from php page) -
Array (thats all the page outputs)
You should use var_dump in stead of echo.
Echo is only for strings, integers, floats and it will print 1 if a boolean is TRUE, and Array for an array.
You could also use print_r() which is a little more readable.
Y don't you convert array into string with some special character as separator and explode it into php?
I've been trying to send a JSON string to PHP server like this:
$.ajax({
type: "POST",
url: "themes.php?page=themeoptions",
data: {structure : JSON.stringify(structure)},
});
However, every quotation mark from the string I send is escaped automatically, so I can't decode it in PHP with json_decode(). I could remove all the escape characters, but I really doubt that this way of sending JSON data to server is safe.
So, I was wondering if you have any ideas on how to do that in a simple and safe (not necessarily bulletfroof) way?
Thank you!
You can just do '{"structure":' + JSON.stringify(structure) + '}' or {structure: structure}
The first one is a JSON String so jQuery doesn't need to parse it. The second is a javascript object, so jQuery knows exactly how to parse it.
But you are mixing the two, so jQuery is confused and re-encodes your object, because you encoded only half your object.
So another alternative would be JSON.stringify({structure: structure })
Try this
$.ajax({
type: "POST",
url: "themes.php?page=themeoptions",
data: {structure: structure},
});
$.ajax({
type: "POST",
url: "themes.php?page=themeoptions",
data: structure,
});
Quoting from the documentation:
dataObject String Data to be sent to the server. It is
converted to a query string, if not already a string. It's appended to
the url for GET-requests. See processData option to prevent this
automatic processing. Object must be Key/Value pairs. If value is an
Array, jQuery serializes multiple values with same key based on the
value of the traditional setting (described below).
I'm trying to return data as an array to my .ajax function on success so I can do do multiple things, but I can't get to work for anything except a single value. Like return $foo. But when I return $array, the page never loads up.
JavaScript's use & utilizing of array is different as compared to PHP's processing. So, either you need to send the data to your AJAX function as a JSON (JavaScript Object Notation), otherwise you need to send the data as a string with a common separator to your AJAX function.
In the latter case, you will need to split up the Response Text in your AJAX function, with that common separator, to make up a JS Array. Then it will be very much easy to use that array instead.
Hope it helps.
You would have to serialize your data into a string. My preference is JSON, so if your PHP version is >= 5.2.0, you'll most likely have access to a json_encode function. This will turn the PHP object into a JSON string. If you don't have access to json_encode, you can use the PECL JSON package.
Looking at the way you reference your ".ajax" function, I am assuming jQuery. So as long as you set the content type to 'json', the JSON response will result in a native JavaScript object. For example,
PHP,
return json_encode(array(1, 2, 3));
JavaScript,
$.ajax({
...
contentType: 'json',
success: function(response) {
for (var i = 0; i < response.length; i++) {
alert(response[i]);
}
});
This code should proceed to alert 1, 2, and then 3.
I haven't verified the code, but the essential parts are all there. A note to have though, a normal indexed array is turned into a JavaScript list while an associative array will be turned into a JavaScript object.
JSON is a good option to send your data array from PHP to Javascript. In PHP side just encode and return your data array as JSON string and in the Javascript side decode that JSON string and use as normal array.
Suppose your data array is like $array, in your PHP code just encode that array using json_encode($array) and return the resulted JSON string.
In your javascript code decode that JSON string using 'eval' function in the success callback function like:
$.ajax({
type: "GET",
url: "test.php",
success: function(data) {
var dataArray = eval('(' + data + ')');
}
});
I think this will help you ...
Siva