1

Maybe this is silly but I was wondering if it is possible to run any scripts, specifically Ajax, within the Controller of .Net MVC.

i.e. Can I wrap this in anything to make it compile and work when this method is called?

    [HttpPost]
    public ActionResult apiLookUp()
    {

     $.ajax({
        url: 'example.com/api',
        type: 'GET',
        dataType: 'json',
        data: {

        },
        success: function (json) {

        },
        error: function (errorThrown) {
            }
    });
        return Json(new { Success = json });
    }
Austin
  • 3,010
  • 23
  • 62
  • 97
  • heyyy....no..never... – Kartikeya Khosla Aug 05 '14 at 12:32
  • 1
    merp, okay. Don't see why the down vote however...valid question isn't it? :) – Austin Aug 05 '14 at 12:34
  • 1
    your controller executes on the server, javascript executes on the client browser. two different worlds, very important to understand – aw04 Aug 05 '14 at 12:35
  • @Austin, it is not. a question like this shows a massive knowledge gap and lack of understanding of how web technologies work. – zaitsman Aug 05 '14 at 12:35
  • @aw04 Is it possible for me to call/reference a JS script from the Controller then? I am trying to keep some .js away from users, thus I am trying to find a way to get the data I want, then just pass that to the visible JS scripts. – Austin Aug 05 '14 at 12:36
  • @zaitsman Well I JUST started learning .net, c#. js, ajax, ect... I have seen all kinds of neat methods and was just wondering. – Austin Aug 05 '14 at 12:36
  • @Austin No, because js is on the client it's not possible to keep away from the user. You should consider doing whatever it is in c# on the server – aw04 Aug 05 '14 at 12:37
  • @Austin - have a look at `HttpClient` - something like this is maybe what you are after http://stackoverflow.com/questions/25039089/bind-model-to-json-api-on-other-server/25039532#25039532 – petelids Aug 05 '14 at 12:38
  • 1
    @Austin Also, it's important to note ajax is used to communicate with the server from the client. So issuing a get, for instance, when your already on the server doesn't make a lot of sense – aw04 Aug 05 '14 at 12:39
  • @aw04 Okay, so its for the "area" of the server, as in all servers. I thought maybe it mean't only to a specific one, as this call will go cross-domain to another API. – Austin Aug 05 '14 at 12:40
  • 1
    Depending on your setup, you can call the api from your mvc controller just as if it's another c# class. You can also call it from the client using a get, which is quite normal, unless as you said you have a reason not to have that information on the client. If the api is external, @Bill Gregg's answer below looks promising. – aw04 Aug 05 '14 at 12:42
  • 2
    @zaitsman this place is a Q/A site, you vote down a question because it is poorly written, we don't penalise because someone lacks knowledge, that just defeats the object of the site. – Mr. Mr. Aug 05 '14 at 13:01

1 Answers1

4

If you are trying to access one of your own resources, then making an AJAX call is not necessary. You are already at the server, and could instantiate the object and make the call directly.

But, if your goal is to call an external site, then yes you can.

 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://someServer.com/example.com/api");
 myReq.ContentType = "application/json; charset=utf-8";
 var response = (HttpWebResponse) myReq.GetResponse();
 string text;

 using (var sr = new StreamReader(response.GetResponseStream()))
 {
     text = sr.ReadToEnd();
 }
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
  • this would be cross domain. My server to another server's API. – Austin Aug 05 '14 at 12:41
  • 1
    Then what I've written is what you want. – Bill Gregg Aug 05 '14 at 12:41
  • Awesome I shall give this a shot! – Austin Aug 05 '14 at 12:42
  • How do I keep the `json` intact as `json`? Using a `string` messes up some of the format. Is there a way to do something like `return Json(new { json = response.GetResponseStream() });`? I just want to return the exact `json` that is acquired from this WebRequest. – Austin Aug 05 '14 at 13:07
  • You can use JSON.Net to deserialize the string back into JSON. – Bill Gregg Aug 05 '14 at 13:24
  • Still struggling to find the correct syntax for de-serializing (sorry still quite new to this stuff) . My latest guess was `return Json(new { json = JsonConvert.DeserializeObject(text) });` and I am trying to return to a JS function's success method. – Austin Aug 05 '14 at 13:34
  • I tried this too `return Json(new { json = JsonConvert.DeserializeObject(text) });` but it just returns an empty braces. – Austin Aug 05 '14 at 13:42
  • Was text empty? If not, then just search for "How to deserialize a string into JSON using JSON.Net" If you are still stuck, then post another question. If text IS empty, then your server call is not ok. – Bill Gregg Aug 05 '14 at 13:55
  • Maybe I will do another question. When I return just text, it add's escaping slashes. But when I deserialize again, it add's all the indentation back to the json. I can't seem to find the right syntax to format it into single-line json object. – Austin Aug 05 '14 at 13:57