1

I'm very new in javascript, I have a problem, I have a external js file which I need to run some c# server side codes. my external js file is something like:

my.login = function(parameter, callback) {
    if(someCondition)
    {
        alert("you cant progress")
    }
    else
    {
        //not importent logic
    }
}

I considered two ways for preparing some condition one of them with ajax call:

$.get("locallhost:2756/myCont/MyAct?Id=" + Id + "", function(response) {
    if (!response.result) {
        alert("you cant progress");
    }

but I get the error $ is not defined another option is using XmlHttpRequest like this:

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;

but I get nothing in resText its "", My controller and action are also like this:

public class myContController : Controller
{      

    [HttpPost]
    public JsonResult MyAct(string Id)
    {
        if (Logic.ValidateId(Id))
        {
            return Json(new { result = true });
        };
        return Json(new { result = false });
    }
}

all I want is validate something in c# and return the result if it's ok or not to javascript, If there is another way, would you please help me?

EDIT: I know I can reference jquery in html files to avoid $ not defined but this is external js that others can use it which they are not in my projects. I need to do something with that external js

feeeper
  • 2,865
  • 4
  • 28
  • 42
someone
  • 535
  • 4
  • 14

2 Answers2

2

you are missing the jquery reference file download it from the below link and reference it in your html file. In src you need to write the path of your jquery.min.js file. If its in the same folder as your html file use below code

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

link : http://jquery.com/download/

Iqbal
  • 291
  • 1
  • 9
  • there is no html file, this is a script that other project can reference it and use it, and i dont access that projects they are external projects – someone Sep 29 '16 at 05:37
  • I need to do something with that external js file – someone Sep 29 '16 at 05:37
  • in that external js file add this function `(function() { // Load the script var script = document.createElement("SCRIPT"); script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; script.type = 'text/javascript'; document.getElementsByTagName("head")[0].appendChild(script); })();` After appending the jquery file you will not get the $ error. – Iqbal Sep 29 '16 at 05:51
1

You can do AJAX request without jQuery. All you need is fix your XMLHttpRequest usage:

function reqListener() {
    console.log(this.responseText);
};

function errListener() {
    console.log(this.responseText);
};

var xhReq = new XMLHttpRequest();
xhReq.addEventListener("load", reqListener);
xhReq.addEventListener("error", errListener); // this works for errors
xhReq.open("POST", "locallhost:2756/myCont/MyAct?Id=" + Id + "", true);
xhReq.send(Id);

Also you can add another callbacks.

You can find more examples on MDN.

feeeper
  • 2,865
  • 4
  • 28
  • 42
  • I'm sry I'm very new to js I dont know how can I determine to show alarm window base on controller response, where should I put that logic? – someone Sep 29 '16 at 06:24
  • @someone into `reqListener` function in the top of my example. – feeeper Sep 29 '16 at 06:31
  • when I press f12 with breakpoint I see that reqListener() function never runs, when I add () after that in xhReq.addEventListener("load", reqListener) it runs but the responsText is undefined:( – someone Sep 29 '16 at 07:02
  • @someone I think that you got some error on your request. I updated my answer. See part about error handling. – feeeper Sep 29 '16 at 07:46