This question already has answers here:
How to make an AJAX call without jQuery?
                                
                                    (22个答案)
                                
                        
                3年前关闭。
            
        

我是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参考文件,请从以下链接下载它,并在html文件中对其进行参考。在src中,您需要编写jquery.min.js文件的路径。如果它与您的html文件位于同一文件夹中,请使用以下代码

   <script src="jquery.min.js"></script>


链接:http://jquery.com/download/

10-06 06:11
查看更多