views:

119

answers:

6

I'm trying to call a page method belonging to a MVC Controller from another site, by means of:

$.ajax({
          type: "GET",
          url: "http://localhost:54953/Home/ola",
          data: "",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(data) {
                console.log(data.Name);
          }
        });

the method code is as follows, really simple, just to test:

 public ActionResult ola()
    {

        return Json(new ActionInfo()
        {
            Name = "ola"
        },JsonRequestBehavior.AllowGet);

    }

I've seen this aproach being suggested here, and I actually like it a lot, should it work...

When I run this, firebug gets a 200 OK, but the data received is null.

I've tried a lot of different approaches, like having the data in text (wish grants me "(an empty string)" instead of just "null") or returning string in the server method...

Can you tell me what am I doing wrong?

Thank you in advance,

João

A: 

Maybe someone much cleverer than me can help. Take a look at Phil Haak's blog post on this subject.

s1mm0t
+1  A: 

Have you tried returning your JSON like so...

public ActionResult ola()
{
    return Json(new { Name = "ola" }, JsonRequestBehavior.AllowGet);
}
tdaines
A: 

You could try returning JsonResult from the controller action method. Method signature would then be public JsonResult ola(). Hope it helps.

Jay Shanker
+1  A: 

Controller:

public ActionResult Ola()
{
    // No need to use a custom ActionInfo type here, an anonymous type
    // will be just fine:
    return Json(new { Name = "ola" }, JsonRequestBehavior.AllowGet);
}

View:

$(function {
    $.getJSON('/home/ola', function(json) {
        alert(json.Name);
    });
});
Darin Dimitrov
A: 

tried all of the above to the same results...

When i try to access the requested address, i get the string or json object the method is returning... but if i do it with the request i get nothing...

must i change something in my web.configs or something?

JLago
A: 

Thanks for all the feedback. I found out that everything I was doing was right and wrong at the same time.

the requests were all functional, but the request was being made to a different domain, wich is automatically blocked by the browsers (except IE). It's a question of security. But since the request was meant to work on a mobile device, when i tested it there, it worked perfectly.

Once again, thanks to everyone who answered, i'll adopt some of the ideas shown here :)

JLago