3

I try to call a ASP MVC action from outside of the domain via ajax.

Setup

I have hosted a MVC application with this action inside:

[HttpPost]
[AllowAnonymous]
public ActionResult AjaxLogin(LoginViewModel model)
{
    [..Login stuff..]
    return Json(new { Url: "...", Result: "..." });
}

Usage

For testing I try a manuell call with a HttpRequester addon from Firefox, with this result:

test call

It is working correct and the answer is as expected. So now I want to made an ajax call from a second web page (different domain).

My jquery (2.2.0) ajax call looks like this:

var requestData = {
    model: {
        Email: emailValue,
        Password: passwordValue
    }
};

var requestPlain = JSON.stringify(requestData);

$.ajax({
    url: json_login_url,
    data: requestData,
    method: 'POST',
    async: false,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (result, textStatus, jqXHR) {
        [...]                     
    },
    error: function (jqXHR, textStatus, errorThrown) {
        [...]
    },
    beforeSend: function (jqXHR, settings) {
        return true;
    },
    complete: function (jqXHR, textStatus) {
    },
});

Problem

The ajax call only gives me an error.

error

SO why is my testcall working but my ajax call not?

Attempts

I also tried a network analysis with the firefox debugging tools.

But I don't understand why it is not working because it shows "status-code 200" but the result is empty!?

network

Steffen Mangold
  • 1,184
  • 1
  • 20
  • 39
  • I am not sure this is the answer but have you tried JSONP - http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about – Dawood Awan Feb 05 '16 at 06:53
  • and another thing is if you are requesting data from another URL - wouldn't it be better to use Web API Controllers? – Dawood Awan Feb 05 '16 at 06:54

1 Answers1

0

This may or may not solve your issue, but if you're making cross-domain AJAX calls, make sure you set up your CORS:

Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method

Community
  • 1
  • 1
Gediminas Masaitis
  • 3,172
  • 14
  • 35