How can I send information from client to server in a widget?

Bushra Syed
Tera Contributor

Hi, 

I am currently trying to send data like so:

Client:

c.data.x="some data";

Server: 

console.log(input.x);

 

I get this error:

Server JavaScript error Cannot read property "x" from undefined

 

I was wondering how I could send it so that the server waits for the data from the client?

 

Thank you!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

send like this; the info message in server should give you Hello

Client Side:

var serverRequest = {};
serverRequest.action = "my_action";
serverRequest.myValue = "Hello";

c.server.get(serverRequest).then(function(r) {
	
})

Server Side:

if(input && input.action == 'my_action'){
	gs.addInfoMessage("My value" + input.myValue);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

send like this; the info message in server should give you Hello

Client Side:

var serverRequest = {};
serverRequest.action = "my_action";
serverRequest.myValue = "Hello";

c.server.get(serverRequest).then(function(r) {
	
})

Server Side:

if(input && input.action == 'my_action'){
	gs.addInfoMessage("My value" + input.myValue);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 
I am Facing the issue  that I'm not able to get the server side data, I used it this Script 

HTML-code 

<div>

<button ng-click="setMseDwn()">SUBMIT</button>
</div>



Client Side Script

api.controller=function($scope) {
/* widget controller */
var c = this;
 $scope.setMseDwn=function(){
var serverRequest =new Object();
serverRequest.action = "my_action";
serverRequest.myValue = "Hello";
 }
console.log(JSON.stringify(serverRequest));
c.server.get(serverRequest).then(function(r) {
 
})
};


Server Side Code

(function() {
if(input && input.action == 'my_action'){
console.log("My value" + input.myValue );
}
})();

Using in the Scoped Application.

Thanks & Regards
Abhijeet Burghate.