【发布时间】:2017-02-07 07:04:12
【问题描述】:
我是 javascript 的新手,我有一个问题,我有一个外部 js 文件,我需要运行一些 c# 服务器端代码。我的外部 js 文件类似于:
my.login = function(parameter, callback) {
if(someCondition)
{
alert("you cant progress")
}
else
{
//not importent logic
}
}
我考虑了两种方法来准备一些条件,其中一种是使用 ajax 调用:
$.get("locallhost:2756/myCont/MyAct?Id=" + Id + "", function(response) {
if (!response.result) {
alert("you cant progress");
}
但我收到错误 $ 未定义 另一种选择是像这样使用 XmlHttpRequest:
var xhReq = new XMLHttpRequest();
xhReq.open("POST", "locallhost:2756/myCont/MyAct?Id=" + Id + "", true);
xhReq.send(Id);
var res = xhReq.response;
var stat= XMLHttpRequest.status;
var resText= xhReq.responseText;
但我在 resText 中什么也没得到它的“”, 我的控制器和动作也是这样的:
public class myContController : Controller
{
[HttpPost]
public JsonResult MyAct(string Id)
{
if (Logic.ValidateId(Id))
{
return Json(new { result = true });
};
return Json(new { result = false });
}
}
我想要的只是在 c# 中验证某些内容,然后将结果返回给 javascript,如果有其他方法,请您帮助我吗?
编辑: 我知道我可以在 html 文件中引用 jquery 以避免 $ 未定义,但这是其他人可以使用的外部 js,而他们不在我的项目中。我需要用那个外部 js 做点什么
【问题讨论】:
-
你是否包含了 jquery($ 实现)??
-
那是什么,我不知道。你能解释一下吗?
标签: javascript c# ajax xmlhttprequest