I have two JavaScript files and I need to send an object from arquivo1.js
to arquivo2.js
via POST, is it possible?
I have two JavaScript files and I need to send an object from arquivo1.js
to arquivo2.js
via POST, is it possible?
What you're trying to do does not make much sense, if you're dealing with JavaScript, make communication via objects and functions. POST is an HTTP verb to communicate with the server. By analogy, it would be like using a cell phone to communicate with someone on your side.
Still, if you need to do POST communication using only JavaScript, there is a jQuery plugin called mockAjax . As the name says, it is used to simulate AJAX requests, especially at the beginning of a Web application development, for unit testing or to maintain separate responsibilities - the front-end developer does not have to worry about what the back-end developer will develop , it simulates server requests to be able to leave the graphical interface working even without a server application. I made a fiddle with an example of this plugin , but the code is simple like this:
$.mockjax({
url: '/url/servico',
dataType: 'json',
response: function (settings) {
// o parâmetro recebido do $.post
var param = settings.data;
this.responseText = {
umaPropriedade: 'random ' + Math.random(),
outraProp: settings.data.foo
};
}
});
var parametro = { foo: 'val'};
$.post('/url/servico', parametro, function(data) {
alert(data.umaPropriedade);
});