Angular 4 And Node js - javascript

In angular 4 at a client side, i have a post method when i click on it ->
var json=JSON.stringify({id:"1",name:"par",title:"ssss"});
var params='json'+json;
this.http.post("http://localhost:3000/users/insertData",params, optio
.subscribe(res => console.log(res.json()));}
after click on this at server side, I am using node express js,
so the problem at server side I am getting data that type which is not acceptable for the database my SQL so data is getting ->
{ 'json{"id":"1","name":"par","title":"ssss"}' : ' ' }
but I want to get data is ->only JSON form
{"id":"1","name":"par","title":"ssss"}
so please give me some solution ...

Try without stringify
var params={id:"1",name:"par",title:"ssss"}

This line: var params='json'+json;
You are concatenating a string with some data in JSON form.
I am not sure what you are trying to do with that, but that is why the data is not JSON when you receive it on the back end.

By default Angular converts all your form submission data into json, even if you submit a model it is converted to json before sending it to the server. Unless you explicitly want to submit form data.
Secondly as pointed out by Adam, what are you trying to achieve using this piece of code var params='json'+json;

Related

How to post and get javascript document into Database

I've a web page in which i've inserted a ACE- javascript Code Editor. I want to post javascript code into my database and then retrieve.
for example in case of JSON we use json.stringify and json.parse to post and retrieve data. I'm usieng SAPUI5 backend is in javascript.
var conf = model.getProperty("/automation-rule-body");
Is there any rule to post javascript code into database ?
If your backend supports REST API use create method of sap.ui.model.odata.ODataModel
var oDataModel = sap.ui.model.odata.ODataModel(sServiceUrl, mParameters);
oDataModel.create(sPath, oData, mParameters);

Check data exist or not using true/false condition by jquery on json formatted data

Hi guys I have some question concern to json_encode of php and jquery $.getJSON.
I have been try to create a notification system and I did that with the help of php and jquery.I can now fetched data from database without a page reload/refresh.
I have fetched data from database and I am displaying them in my website like :-
Jquery code
$.getJSON("notifi.php",function(data){
$(".display_noti").empty(); // Clear out the div
$.each(data.result,function(){
$(".display_noti").append("<div class='palnotific'>You got a
friend request from <strong>"+this['from']+"</strong><br></div>");
});
});
On php side i'm just fetching data from a table and I encoded those in json format at end which isn't concern to my question.
This is what my php produce from that notifi.php file where I did my those fetched and encoded on json format process.
Json encoded format
{"result":[{"from":"hancy061","to":"souraan061"}]}
My question is now on my jquery side using if and else condition how can I check whether there is value data or not in that notifi.php or we can say on that json format data?
If you know whether data is returned empty json data or having data then try checking it like so :
$.getJSON("notifi.php",function(data){
// you can do checking here
if ( data && data.length > 0 ) {
$(".display_noti").empty(); // Clear out the div
$.each(data.result,function(){
$(".display_noti").append("<div class='palnotific'>You got a
friend request from <strong>"+this['from']+"</strong><br></div>");
});
}
else {
// do something here if no data
}
});
Or maybe data returned some value even there is an empty data inside properties as example just returned {"result":[]}, then do checking it by data.result.length
You can use something like
if(data!=null && data!=undefined && data.result!=undefined && data.result.length!=undefined && data.result.length>0){
//Put your loop here
}
folks correct me if I am wrong here.

Retrive post data back using javascript/jquery

I want to pass the object to the new page by using only client side script(javascript),
and I want to read the data back when the new page is loads, but I could not find any tutorial or method to get it.
function postToURL(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
});
// The form needs to be a part of the document in
// order for us to be able to submit it.
$(document.body).append(form);
form.submit();
}
The POST message sent to the server to retrieve the current page is not exposed, by the browser, to JavaScript in that page, so you cannot.
Pass the data via some other technique (such as through the URL, or localstorage).
Yeah, POST data is only handled and seen by the server side (ex. PHP). Javascript is on the client side so it will never be able to see that data unless the POST data first gets passed to server side and that server side in turn hands it off to the client side (ex. Javascript).
Consider using GET as your method in your code above so that you can pass the parameters inside the actual URL to make it accessible to the browser / client / Javascript.
parameters can be parsed from a JSON-encoded string. If you build your parameters programmically (in the previous step) like:
parameters = {};
parameters['USA'] = 'Washington DC';
parameters['UK'] = 'London';
You can create a flat JSON String representing your structured data by:
parametersString = JSON.stringify(parameters);
and end up with something like this:
"{'USA': 'Washington DC', 'UK': 'London'}"
You can store this string in a cookie, or in a query string or fragment part of the URL.
Your next page can read back this value on the following page, and re-create the structured parameters by decoding the JSON string like:
parameters = JSON.parse(parametersString);
and you're getting the original data back, on which you can run JQuery's each.

Error while writing a Google CCS server in node using node-xmpp

This is python code in Google documentation of writing a CCS server:
https://developer.android.com/google/gcm/gs.html#server
I figured out most of it, and how to code it in Javascript using https://github.com/astro/node-xmpp
But I'm failing to understand how to send data using templates, precisely this part of the code:
def send(json_dict):
template = ("<message><gcm xmlns='google:mobile:data'>{1}</gcm></message>")
client.send(xmpp.protocol.Message(
node=template.format(client.Bind.bound[0], json.dumps(json_dict))))
where in node-xmpp, a send is done by this:
var cl = new xmpp.Client({ jid: username,
password: password });
cl.addListener('online',
function() {
argv.slice(5).forEach(
function(to) {
cl.send(new xmpp.Element('message',
{ to: to,
type: 'chat'}).
c('body').
t(argv[4]));
});
I understand the JSON being sent, But I'm not able to do the binding of the template that they are managing in Python. Any help?
The important part is to send the message in the required format :
<message id="">
<gcm xmlns="google:mobile:data">
{
"to":"REGISTRATION_ID", // "to" replaces "registration_ids"
"message_id":"m-1366082849205" // new required field
"data":
{
"hello":"world",
}
"time_to_live":"600",
"delay_while_idle": true/false
}
</gcm>
</message>
It doesn't matter if you use a template or not. I don't know python nor javascript, but the purpose of the template in the python example seems to be simply to avoid the need to write the xml tags that wrap the JSON every time you send a message. You can append them to the JSON when you send the message.

how to send a single element from browser to server without reload

How can I send just one element from browser to server fast and without reloading browser page?
Is there an AJAX way to do this, that is a NON-FILE method? The opposite of ".load"?
.load works great sending a single element from server to browser without page reload.
How to do the opposite direction?
Browser is JavaScript.
Server is vxworks with windmarks.
PRESENT METHOD THAT WORKS BUT RELOADS PAGE:
Presently, the browser element is and I use submit to send it to the server, but this takes too long and reloads the browser page.
The element's innerHTML contains data formatted as a vxworks WINDMARK.
(When the vxworks server receives this submission, it reads the windmark and copies it to a 'C' string for backend software to process.)
If you're using jQuery and PHP then something like this should work:
JS:
$.ajax('doServerSuff.php?action1=saveLog', function() {
// do stuff after server received the data
});
PHP (doServerStuff.php):
<?php
if ($_GET['action1'] == 'saveLog') {
//do stuff
}
?>
You can get and send data using jQuery. using something like this:
$.post('urlfromserver',browserdata,function(datafromserver){
//do stuff
})
if you let me put a little bit more, it's a good idea to use JSON to send/receive data to/from server. Having that in mind, you can do something like:
$.post('urlfromserver',{browserdata: JSON.stringify(browserdata)},function(datafromserver){
javascriptObject = jQuery.parseJSON(datafromserver)
//do stuff
})
And in your PHP code, it would be as simple as using json_encode to send data to javascript and json_decode to receive data from javascript
UPDATE
Obtaining the data in the server should be as simple as requesting the object via post or get depending on your send method, and parsing the JSON.
In PHP, this is an example of obtaining data using the above code:
$dataFromBrowser = json_decode($_POST['browserdata'])
Use $.post in jQuery to send the data.
The load intruction can also be used to transmit data to the server:
$().load("url", {limit: 25}, function(){
//transmission finished
});
In this example the parameter limit with the value 25 wil be transmitted to the server.
Taken from:http://api.jquery.com/load/
You can simply do with the ajax call like this
$.ajax({
type: "POST",
url: "yourpage.php?id=someValue",
success:function(data){
//do some stuff here
});
I got it working. Here is the corrected JavaScript, from the above answer.
Here is how to change a single element at the server.
This is also an example of how to write to a vxworks windmark string at a vxworks web server, without reloading the page.
NOTE: no URL is specified, so that the same page is used.
$.ajax({
type: "POST",
url: "?general_page_to_MM_data_1_windmark=CCCCCCCCCCCCCC",
success:function(data_from_server){
}});

Categories

Resources