This question already has answers here:
Query String generated by $.param() include square brackets for array
(2 answers)
Closed 7 years ago.
Currently I do some code refactoring and try replace generation of query string by concatentation to serializing of json object
From:
$.ajax({
url:'./services/DataService/getDetails?metric=9&'+dashBoards.getFilter()+'groupby=quarter&'+dashBoards.options.time.start1+'&'+dashBoards.options.time.end1+'&peergroup='+dashBoards.options.peerGroup,
type:"GET",
To:
$.ajax({
url:'./services/DataService/getDetails',
data: jsonObject,
type:"GET",
Almost everything works fine, except one thing. If jsonObject contains array field it looks in query string like that:
...?metric[]=1&metric[]=3
Instead of
...?metric=1&metric=3
Is there way how can I fix it? Thanks!
You can fix it adding to $.ajax parameter traditional: true.
Here you can find reference why
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
The JSON object I got was from another PHP file that is called through $.ajax(). For example, I returned from my PHP file a echo json_encode(array('a' => 'b')).
Then, I have the following $.ajax() code:
let objKey = ['a'];
$.ajax({
type : 'POST',
url : 'phpfilehere.php',
dataType : 'json',
success : function(obj) {
alert(obj.objKey[0]);
}
});
It should have alerted b instead of undefined. Then, I tried alert(obj.a) and it worked. It alerted b. How do I access the value of the JSON object with an array of string which all corresponds to the key of said JSON object?
obj.objKey[0] is false in your case, it's good if your object is like :
obj = { 'objKey': ['b'] }
You have two solutions in your case
alert(obj[objKey[0]]);
or
alert(obj.a);
Reference
Property Accessors MDN
This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 4 years ago.
Sorry if the title is confusing, I'm confused my self.
So its like this, I get a string from api like this: 'data.data.type'.
And I need to turn it into this response['data]['data]['type'].
Any idea how I can achieve this?
Assuming response is a single object , you can get the value of type like this
var response = {
data: {
data: {
type: "Here is type"
}
}
}
console.log(response['data']['data']['type'])
This question already has answers here:
How to serialize a raw json field?
(2 answers)
Closed 5 years ago.
I am creating a javascript object from a c# object and one of the properties is a reference to a js function , but when serializing the object the value has quotes around it witch makes it a normal string and not a function.
this is the current output :
{ "x": "functionNameToBeCalled" }
But I need it to be like
{ "x": functionNameToBeCalled }
Is there anyway to do this with Json.Net or do I have to create the js object manually?
I tried using the JsonPropertyAttribute but can't figure out which property to set!!!
change the way of calling your method, something like this:
window.z= function(){ console.log('hi');}
var b = { a: 'z'}
window[b.a]();
so no need to change json serialization behavior.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Passing javascript variables to php?
(5 answers)
Closed 8 years ago.
let's say i have:
<script>
var jsString="hello";
</script>
and i want it to pass into php string:
$phpString = jsString;
how do i do that correctly?
please tell me the right way. thanks in advance.
You need a Ajax call to pass the JS value into php variable
JS Code will be (your js file)
var jsString="hello";
$.ajax({
url: "ajax.php",
type: "post",
data: jsString
});
And in ajax.php (your php file) code will be
$phpString = $_POST['data']; // assign hello to phpString
You will need to use an HTTP POST to send the data to PHP. Check out this tutorial: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php to see how to send a post without JQuery. Also see the XMLHTTPRequest docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest. As other answers have noted, JQuery is the makes this much easier with $.post: http://api.jquery.com/jquery.post/.
To get the string in PHP use the $_POST variable.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Javascript Objects: Dynamic variable names?
I have a json string passed in from a cgi script. This json string has a list of id's. In javascript
var informationObj = jQuery.parseJSON(information);
tid = informationObj.idList[0].id;
tid is now an ID and I want to use it to access objects within the json string itself like so:
alert (informationObj.tid.rpath);
However this does not seem to work. I have also tried:
alert (informationObj.eval(tid).rpath);
Is there a way around this?
Thanks.
You need this form:
informationObj[tid].rpath
They are equivalent:
var a = 'something';
b[a] === b.something
Use bracket notation:
alert(informationObj[tid].rpath);