Accessing python dictionary in javascript [duplicate] - javascript

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
I am sending dictionary from python like this:
{'root':['value','path','type'],.......}
i am send it to javascript by ajax call request by serializing it,
How to access that dictionary in javascript.
thanks in Advance

Suppose you are making AJAX call in below way, you will get the response dict as resValue. use JSON.parse method on it
$.getJSON( "/url", {params }, function( data, status, xhr ) {
$.each(data.response, function(resKey, resValue){
if(resKey == "success"){
var _result = JSON.parse(resValue);
}
}
}

Related

Passing js variable to laravel model [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Accessing $_COOKIE immediately after setcookie()
(9 answers)
Closed 3 years ago.
I am creating ajax model update request, after this I want to refresh my compacted variable with ajax response. I need to do these tasks without page reload. If anyone have any ideas please share with me.
My code:
success: function(result) {
console.log(result);
var abc = JSON.stringify(result);
Cookies.set('clients', abc, 10);
$('#cookie').html(Cookies.get("clients"));
var abc2 = $('#cookie').text();
<?php $client1 = $_COOKIE['clients']?>
I have been trying to do it with cookies, but as I know, to get recent cookies need reload page.

How to serialize Json javascript [duplicate]

This question already has answers here:
Serialize javascript object to json and back
(4 answers)
Closed 7 years ago.
I need to serialize an object to JSON, in javascript.
How I can do it? I google it, but im not found the solution.
All the response are "You mustn't serialize Json in javascript, you must serialize in other language as Java, C#, so on"
Without use JQuery.
Checkout JSON.stringify() and JSON.parse() in JSON2 documentation
Example:
myData = JSON.parse(text); // from json string to js object
var myJSONText = JSON.stringify(myObject, replacer); // js object to json string
if you want to know about more checkout this link
Serializing to JSON in jQuery

AngularJS: Get value from JSON data [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
I'm trying to get value from response JSON data. How can i get the values domain, status, key.
Response JSON data:
{"example.com":{"status":"YES","key":"example"}}
Angular JS Code
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
alert();
}).error(function() {
});
use angular.fromJson to parse JSON then traverse it using .
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
var data = angular.fromJson(response);
console.log(data['example.com'].status);
}).error(function() {
});

Access JavaScript array object in PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have a JSON data about 150 users, i have convert that into java script array, now what i want it to pass this array to PHP, So that i can perform further operations on this array.
var JSONString = '[{'user_id' : "1", "user_name" : "abc", "email":"abc#gmail.com"}, {"user_id":"2", "user_name":"abc", "email":"abc2#gmail.com"}]'
var JSONObject = JSON.parse(JSONString);
for (var key in JSONObject) {
if (JSONObject.hasOwnProperty(key)) {
console.log(JSONObject[key]["user_id"] + ", " + JSONObject[key]["user_name"]);
}
}
now what i need it to pass this array to PHP array variable , that will loop through and perform further operation.
i have seen some solution that display it on html with JavaScript, but i don't need that. i need this array object in php array variable.
Instead of parsing JSON string in JavaScript, pass it directly to PHP and use
PHP function
json_decode($json, true)
it will give you data in array format.

jquery compare arrays $.getJSON [duplicate]

This question already has answers here:
Variable doesn't get returned from AJAX function
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I am trying to compare 2 arrays. One is taken from DOM, generated by .split(" "), the other I would like to be retrived by $.get call.
All is working fine if I test the dictionary array manually. Example:
// var dictionary = ["in", "the"]; // <- this one works
var dictionary = $.getJSON('job/dictionary/en');
$(this).keyup(function() {
var words = $(this)
.text()
.split(" "),
words_final = [],
i = 0;
jQuery.grep(words, function(word) {
if (jQuery.inArray(word, dictionary) == -1) words_final.push(word);
i++;
});
});
Response from this URL is Content-Type: application/json with response body:
[
"in",
"the"
]
Apparently... then I try to run $.getJSON('job/dictionary/en') in chrome dev tools, I get an object back with responseText: "["in","the"]"
I've tried with $.get() as well with the same result.
Any help really appreciated.

Categories

Resources