有这样的代码结构:
SkillEidt.js(Javascript文件):
var SkillEdit = ({
_designtemplate: ["", "input", "dropdownlist", "checkbox"],
_designTemplateData: {},
readValue: function() {
/* when try to read value from customer.Html it's null */
return this._designTemplateData;;
},
RequestResponse: function (data) {
/* able to get and set value from ajax call */
this._designTemplateData = data;
},
ajaxCall : function() {
$.ajax({
url: "/VendorDetails/GetVendorDetails",
type: "POST",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (result) {
alert(result.statusText);
},
success: function (result) {
requestReponse(result);
}
});
});
SkillEdit.ajaxCall() /* very important to set _designTemplateData data */
Customer.Html(页面):
<script src="~/Scripts/SkillEdit.js"></script>
<script type="text/javascript">
function SomeBuuttonClickEvent() {
var notAbleToGetValue = SkillEdit.readValue();
}
</script>
------------------------------------------------------------------------
调试时,SkillEdit.ajaxCall()
会调用ajaxCall()
方法,成功时会调用RequestResponse
并设置_designTemplateData
。但是当我在Customer.Html
页readValue
上单击按钮(SomeBuuttonClickEvent)时返回空值。如何设置_designTemplateData
数据。
..
添加了$ .ajax功能。如何使用任何解决方案解决问题
需要在里面写下什么代码:
RequestResponse:函数(数据) 要么 ajaxCall:function()
答案 0 :(得分:1)
请试试这个。 SkillEdit.js
var SkillEdit = function(){
_designtemplate= ["", "input", "dropdownlist", "checkbox"],
_designTemplateData= {},
readValue = function () {
$('#templabel').text(_designTemplateData);
return _designTemplateData;
},
RequestResponse = function (data) {
debugger;
return _designTemplateData = data;
},
ajaxCall= function () {
//return this.RequestResponse(["12", "13"]);
$.ajax({
url: "/Home/GetVendorDetails",
type: "POST",
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (result) {
alert(result.statusText);
},
success: function (result) {
RequestResponse(result);
console.log(result);
}
});
}
return {
readValue: readValue,
RequestResponse: RequestResponse,
ajaxCall : ajaxCall
}
}();
在ViewPage中:
<script src="~/js/SkillEdit.js"></script>
<label id="templabel"></label>
<input type="button" value="stake overflow question" onclick="return SkillEdit.ajaxCall();"/>
<input type="button" value="read Value answer" onclick="return SkillEdit.readValue();" />
在控制器中:
[HttpPost]
public ActionResult GetVendorDetails()
{
return Json("[12,13]");
}
答案 1 :(得分:0)
您的代码似乎正在运行:(在下面运行)
var SkillEdit = {
_designtemplate: ["", "input", "dropdownlist", "checkbox"],
_designTemplateData: {},
readValue: function() {
return this._designTemplateData;;
},
RequestResponse: function (data) {
this._designTemplateData = data;
},
ajaxCall : function() {
this.RequestResponse("test successful");
}
};
SkillEdit.ajaxCall()
alert(SkillEdit.readValue()) // Should alert "Test successful"
&#13;
您的错误可能在于您的ajax响应的处理。