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

2
  • Did youve included jquery ($ implementation)?? Commented Sep 29, 2016 at 5:30
  • what is that, I dont know. would you please explain more? Commented Sep 29, 2016 at 5:31

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

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
I need to do something with that external js file
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.
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.

4 Comments

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 into reqListener function in the top of my example.
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 I think that you got some error on your request. I updated my answer. See part about error handling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.