Send object via POST between Javascript

Asked

Viewed 1,263 times

0

I have two Javascript files and need to send an object arquivo1.js to the arquivo2.js via POST, this is possible?

  • Can you explain the functionality you’re implementing and where you need it? ajax between javascript files doesn’t make much sense ( unless you’re talking about Nodejs)

  • Well, it’s just that I’m generating several buttons through one for in the onclick of it I want to send an object, but it hasn’t worked very well, only when I send a property only.

1 answer

2


What you’re trying to do doesn’t make much sense, if you’re dealing with Javascript, communicate via objects and functions. POST is an HTTP verb for communicating with the server. Making an analogy, it would be like using a cell phone to communicate with someone who is on your side.


Still, if you need to communicate via POST using Javascript only, there is a jQuery plugin called mockAjax. As the name says, it is used to simulate AJAX requests, especially at the beginning of the development of a web application, for unit testing or to maintain separate responsibilities - the front-end developer need not worry about what the back-up developerend will develop, it simulates server requests to be able to let the graphical interface work even without a server application. I made a fiddle with an example of this plugin, but the code is that simple:

 $.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);
 });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.