I need a function to return the current instance as a json text, because I will send the values with an ajax request to server side script.
我需要一个函数将当前实例作为json文本返回,因为我将带有ajax请求的值发送到服务器端脚本。
I don't know where to use the "this" keyword in a jquery selector
我不知道在jquery选择器中何处使用“this”关键字
function Actor(){
this.input=function(pname,ppassword){
this.name=pname;
this.password=ppassword;
}
//I need a function to return the current instance as a json text here
//I will send the values with an ajax request to server side script with a function
}
I've found out that jquery doesn't support encoding as JSON and I need to use JSON2.js for serializing as JSON string.
我发现jquery不支持编码为JSON,我需要使用JSON2.js作为JSON字符串进行序列化。
New browsers have native JSON support so you can use JSON.stringify without including JSON2.js
新浏览器具有本机JSON支持,因此您可以在不包含JSON2.js的情况下使用JSON.stringify
1 个解决方案
#1
2
You may need to be more specific in your question but if you need this in the input function to be a JSON string, you can use:
您可能需要在您的问题中更具体,但如果您在输入函数中需要这个是JSON字符串,您可以使用:
function Actor(){
this.input=function(pname,ppassword){
this.name=pname;
this.password=ppassword;
return JSON.stringify(this);//this will return pname and ppassword in json object string
}
}
See here for JSON libraries for older browsers that don't support JSON object natively: https://github.com/douglascrockford/JSON-js
请参阅此处了解本机不支持JSON对象的旧浏览器的JSON库:https://github.com/douglascrockford/JSON-js
#1
2
You may need to be more specific in your question but if you need this in the input function to be a JSON string, you can use:
您可能需要在您的问题中更具体,但如果您在输入函数中需要这个是JSON字符串,您可以使用:
function Actor(){
this.input=function(pname,ppassword){
this.name=pname;
this.password=ppassword;
return JSON.stringify(this);//this will return pname and ppassword in json object string
}
}
See here for JSON libraries for older browsers that don't support JSON object natively: https://github.com/douglascrockford/JSON-js
请参阅此处了解本机不支持JSON对象的旧浏览器的JSON库:https://github.com/douglascrockford/JSON-js