Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Javascript PostMessage with JSON object as data

I have to post JSON object as data using javascript PostMessage method, below is the object that I would like to post:

{ 
   "Type" : "Login" 
}

I believe this method is used to between iframe implemented inside a website, the iframe source URL is different from the parent window, how do I implement it in code?

I have this sample code I used, but I don't understand how to it work and how can I test it. Appreciate if someone can help!!

Code:

    @{
        string jsonParameter = Newtonsoft.Json.JsonConvert.SerializeObject("Login");
    }

    var jsonData = "@Html.Raw(jsonParameter)";
    parent.postMessage({ "Type": jsonData }, "*");
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are correct, PostMessage is used to send messages between ifames. The code you have given in your sample is intended to be used in a child iframe and it is sending a message to the parent iframe.

In the parent iframe You also need to listen for messages coming in, for example

window.addEventListener("message", (event) => {
  console.log("Received data from child iframe ", event.data);
});

Assuming you are sending a static message, the code sample you have given that sends a message to the parent iframe can also be simplified as there is no need for the c#

parent.postMessage({ "Type": ""Login }, "*");

Please also note the second parameter to PostMessage should be an expected domain instead of "*" as it prevents malicious sites from intercepting messages. I would recomend reading the mdn page for more information https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda