i am new to nativescript angular2 programming. I want to post large json data to a server. but when i create a json format data, data becomes truncated. I think reason is the size of the holding variable.
//my code is:
let headers = new Headers({ "Content-Type": "application/json" });
let options = new RequestOptions({ headers: headers });
let body=
{
"id": "1002345",
"weekday": [{
"item1" :" data1",//string
"item2":" data2",//string
"item3":" data3",//string
"item4":" data4",//string
"item5":" data5",//string
"item6":" data6",//base64 string of an image
"item7":" data7"//base64 string of an image
}]
};
when i try to print body then data becomes truncated!!
1. Which variable can hold large data?
2. Any alternate method to post large data?
Please help me out.
//json Format:
{"id":"1002345","weekday":{"item1":"nil","item2":"nil","item3":"nil","item4":"nil","item5":0,"item6":"nil","item7":"nil"}}
//actual Json data:
{"id":"1002345","weekday":{"item1":"nil","item2":"nil","item3":"nil","item4":"nil","item5":0,"item6":"iVBORw0KGgoAAAANSUhEUgAAAlgAAAEsCAYAAAAfPc2WAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAABxpRE9UAAAAAgAAAAAAAACWAAAAKAAAAJYAAACWAAAQl/KErzoAABBjSURBVHgB7N0HrHRFFQBgioIovaggBA0gAQOKYCUYCSKCFWxgQFHsiEYg1sQaJUYUhQgCIoaggtg7KCJFBYTYEguiQAgWooKoIBb0HH0L6/PM//+7b997u/d+k5zsvrlz5875IPlPdu/OXW01jQABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIE
here item6 s some part and item7 is missing!
If by "when i try to print body" you mean you output it with console.log() you need to be aware that on most systems (including Chrome, Firefox, IE, even nodejs) the debug output from console.log() is truncated to prevent it going over a reasonable length for display.
Unless you have some other reason to believe your variable has truncated, it probably hasn't been.
Related
let jsonString = JSON.stringify(json);
console.log(jsonString); //prints {"5667787":"currentTaxless":99.82,"current":123.78}}
GM.xmlHttpRequest({
method: "POST",
url: "https://exampleau.tld",
data: jsonString,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
//stuff
}
});
Hi,
I am starting to feel myself stupid. I haven't found any way to feed into data regular object/array or json, no matter what I did (changed headers, added dataType, feed that json variable) - the data was not posted. Only this solution posts data. In the Greasemonkey documentation there is nothing about feeding plain json.
The problem is, that on backend, when I receive such a data - it is:
an array with single key and no value
the key is html_entity_encode(d) string
where dots in float numbers are replaced with underscores. This is what I am getting:
{"5667787":{"currentTaxless":99_82,"current":123_78}}
Qusetion: What am I doing wrong or how to post without a hassle or receive normally formatted posted data without a hassle using Greasemonkey???
Versions: Greasemonkey v4.11
Firefox v81
Nevermind.
The solution is not to
data: jsonString,,
but rather explicitly put json string under some key as value like this
data: 'data=' + jsonString,
I am attempting to implement a large searchable table of information on a static website- so using SQL or PHP is not possible. I am considering using Datatables, and converting the CSV into HTML. However I feel that having a nearly 3000 long HTML table isn't the most efficient way of doing this? What would be the best way of doing this? Thanks.
Have two files, an HTML page (i.e. the datatable your users will use) and a JSON file where you will store all your data.
Then, use fetch() to retrieve the data from the JSON file into the HTML page.
Say you wanted to display a datatable with two fields - names and DOBs - your JSON would look something like this:
{
[
["John Doe", "5.4.1996"],
["Jane Doe", "5.4.2006"]
]
}
On the HTML page:
let response = await fetch(url);
if (response.ok) { // if HTTP-status is 200-299
// get the response body (the method explained below)
let json = await response.json();
// TODO: put the retrieved json into the datable
} else {
alert("HTTP-Error: " + response.status);
}
In our web app, we have options that can be changed in using a POST HTTP request. There are a good number of options, and I will be writing a new test for each one, so I don't want to use the UI to change each option, seeing as there are 150 of them. SO my idea was to set up a custom command that I could feed arguments into (the argument being which option I want to update, and the new value for that option).
I put the list of options in a fixture, so it is in a JSON object. I was able to get to the point where I can find the key I'm looking for and update the value from the fixture, but I am running into an issue where my cy.request won't actually send any data. I've tried updating the headers, updating the body, setting json:true. Nothing works. So I'm hoping someone here will have some advice.
//fixture.json
{
"option1":"on",
"option2":"off",
"option3":"off
}
//commands.js
Cypress.Commands.add('update_options',(option, newValue) => {
cy.fixture('fixture.json').then((oldBody)=>{
let newBody = Objects.assign({},oldBody);//copy old options list into new object
function replace(option, newBody){
newBody[option]=newValue;
}
replace(option,newValue);
cy.request({
method:'POST',
url:'myURLwithParams',
form: true,
json: true,
body: newBody
})
});
});
//spec.js
cy.update_options("options1", "off");
I can get the new object with the updated code and everything, so that all works. The only thing I can't figure out is how to get it to actually POST. The JSON just doesn't compile correctly. I tried JSON.stringify(newBody) - no luck. I've tried every combination of everything I've mentioned and can't get it to work.
I tried with below code (with some hard coded data) and it works for me,
cy.fixture("fixture").then((oldBody) => {
cy.log(oldBody);
let newBody = oldBody
newBody['option1'] = 'DUMMY_DATA';
cy.log(newBody);
cy.request({
method: "POST",
url: "myURLwithParams",
form: true,
json: true,
body: newBody
});
});
Notable changes:
Directly assigned the old JSON object to a new JSON object (Instead
of Object usage)
Put some logs to track the changes
For your reference, attaching some of the screenshots here,
New JSON data (post substitution):
XHR request sending updated JSON:
The task is rather simple, I request the endpoint with POST request (https://banana.com/endpoint/swap.php), give it my form: { banana: ["China's Red", "Sweden's Gray"], apples: [] } and send it.
However, the Request module for NodeJS that I am using does not encode the empty array (in this case "apples") and if the endpoint doesn't receive the "apples" array, it returns an error - "Invalid JSON". I have tried doing this with already encoded strings and it works just fine. I am also unable to stringify my json and then use encodeURI(), as it will then give "bananas" and "apples" quotes around them, which will get encoded - needless to say, the endpoint doesn't like that either.
I'd really appreciate if somebody could at least point me in the right direction. As I am unsure on how to proceed with this, without creating some awful spaghetti code.
data = { banana: ["China's Red", "Sweden's Gray"], apples: [] }
result = JSON.parse(JSON.stringify(data)) .
You wouldn't get double around banana and apple and if you need to access then access it
console.log(result.banana)
console.log(result.apple)
So if you need to feed this result in post request then -
url = 'your url';
const options = {
url: url,
method: 'POST',
headers: {
Accept: 'application/json',
'Accept-Charset': 'utf-8'
},
json: result
};
request.post(options, function (err, response, body) {
// do something with your data
})
Let me know if this works.
I am trying to figure out how to plot data from a local '.JSON' file using angular2-highcharts example.
I followed the example in 'https://www.npmjs.com/package/angular2-highcharts' to first understand how to plot .JSON data and it worked. I took the data available for the example and created a local .JSON file (copied the content from 'https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK' in notepad and saved it with UTF-8 encoding as a .JSON file), and replaced the file path for the JSON request to this. When I do this though, I get an error - response with status 200.
constructor(jsonp : Jsonp) {
//jsonp.get('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK').subscribe(res => {
jsonp.get('./data.json').subscribe(res => {
this.options = {
title : { text : 'AAPL Stock Price' },
series : [{
name : 'AAPL',
data : res.json(),
tooltip: {
valueDecimals: 2
}
}]
};
});
}
options: Object;
};
Since I am not super familiar with json data/ Javascript or angular2 I am not sure if I am missing something very basic here. Any help is appreciated.
as far as I know, Response Status 200 specifies that request was successful. i.e. your request was successfully handled. perhaps you want to try checking response data.
check your callback for response data.
Using http instead of json helped. I made use of the suggestion in this answer https://stackoverflow.com/a/36305814/4567096.