我很新的JavaScript,我有一個問題,我有一個外部js文件,我需要運行一些c#服務器端代碼。我的外部js文件是一樣的東西:從外部JavaScript文件調用c#邏輯
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($執行)? –
那是什麼,我不知道。你能解釋更多嗎? – someone