简体   繁体   English

如何将JavaScript函数中的变量值分配给Mason变量?

[英]How to assign the value of the variable in JavaScript function into a Mason variable?

I am getting window.location.href property on click of a button on client side in a javascript variable. 单击javascript变量中客户端上的按钮时,我正在获取window.location.href属性。 My requirement is to send it back to server. 我的要求是将其发送回服务器。 How can I get the JavaScript variable value back in Mason code? 如何在Mason代码中获取JavaScript变量值?

One option (which I have implemented currently) is to dynamically create a hidden text field with value set to window.location.href , and do a form submit. 一种选择(我目前已经实现)是动态创建一个隐藏的文本字段,其值设置为window.location.href ,然后进行表单提交。

How can use ajax here? 如何在这里使用ajax? I am looking for a ajax solution, how it is different from form.submit() . 我正在寻找ajax解决方案,它与form.submit()不同。

The whole point of the problem is that javascript variable is available to js interpreter in the browser and not to the server side script. 问题的全部在于,JavaScript变量可用于浏览器中的js解释器,而不适用于服务器端脚本。 You need to somehow get the variable contents and send it back to server. 您需要以某种方式获取变量内容并将其发送回服务器。 This can be done either via form (which will at least reload the page) or XHR (main technology behind ajax). 这可以通过表单(至少会重新加载页面)或XHR(ajax背后的主要技术)来完成。

I never did XHR directly, but using dojo framework it can look like: 我从未直接做过XHR,但是使用dojo框架看起来像:

dojo.xhrPost( {
    handleAs: "json",
    url:      "http://example.com/whatever",
    content:  { "data_url" : window.location.href }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM