Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

javascript - setting hidden text from server-Node.js(express)/Asp.net-core to read it from client- angular 2

My(assumption) flow is I'll get a form post method html which will contains TOKEN catching it in server side.

    app.post('/callback', (req, res)=> {
    var token = req.body.access_token
    res.cookie('access',token); //instead, i want to set in variable/text field
//res.send('<input type=text name="access_token" value="token" hidden/>')
})

Now, I want to get the token from the variable/text-field from client side which is set by server.

  1. Is it possible to set a value in a text field? (if so, how?)

  2. Is it possible for client side to read a value which is set by the server side?

  3. Is it a correct process?

  4. If any better process, free to suggest.

Update : I Just wanna try it in Asp.net core

  1. Want to save token in varaible at Controller like

    {
    
      if (!string.IsNullOrEmpty(Request.Form["access_token"]))
      {
        var token = Request.Form["access_token"];
    
        ViewBag.Message = token.ToString();
      }
      return View();
    }
    

View part :

@{
    ViewData["Title"] = "CustomeView";
}


<script>
  var message = "@ViewBag.Message";
  console.log(message);
</script>

<a href="@Url.Content("/")">Home</a>

After a click on Home link it will redirect to my angular2 index.html

  1. Index.html - angular 2 , How to use message(Token Value) variable in angular 2 ?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is it possible to set a value in a text field? (if so, how?)

Yes just concatenate token to input value.

app.post('/callback', (req, res)=> {
    var token = req.body.access_token
    res.cookie('access',token); //instead, i want to set in variable/text field
res.send('<input type=text name="access_token" value='+token+' hidden/>')
})

OR ES6 style:

 res.send(`<input type=text name="access_token" value="${token}" hidden/>`)

${expression}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...