0

I am trying to send AJAX POST request to an MVC Application

    $.ajax({

        type: 'POST',
        dataType: 'json',
        data: {"FirstName":"chris","LastName":"cane"},
        contentType: 'application/json',
        url: "http://dev.irp.com/irp.Ajax.Search/home/Foo",
        success: function (data) {
            alert(data);
        }
    });

This script is present on a different server on an ASP.NET application. My MVC App to handle the code is as below

    [HttpPost]
    public JsonResult Foo(fromclient test)
    {
        var obj = new SearchMemberServiceClient();
        var members = obj.FindMember(test.FirstName, test.LastName, "", "", "", "").Members;

        IEnumerable<Bar> sorted =
            from a in members
            orderby a.FirstName ascending
            group a by new
            {
                a.FormattedFullName,
                a.MembershipsProxy[0].GoodFromDate,
                a.MembershipsProxy[0].GoodThroughDate,
            } into k
            select new Bar
            {
                FormattedName = k.Key.FormattedFullName,
                goodfrom = k.Key.GoodFromDate,
                goodthru = k.Key.GoodThroughDate,
            };
        return Json(sorted.ToList());
    }

public class Bar
{
    public string FormattedName { get; set; }

    public DateTime goodfrom { get; set; }

    public DateTime goodthru { get; set; }
}
public class fromclient
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

The problem is the script needs to post to that url and get the json data. But as the controller does not have any view, When I look inside the console on the client side it says 404 error for the url and also it says XMLHttpRequest cannot load http://dev.irp.com/irp.Ajax.Search/home/Foo. Origin http://web-dev.irps.com is not allowed by Access-Control-Allow-Origin.

I dont know if the problem has to do with the absolute path of the url for ajax request. If so how could I overcome this?

2
  • I assume the "foo" is a typo (dev.irp.com/irp.Ajax.Search/home/foo) instead of Foo. If so, please change it to eleminate any confusion. Commented Feb 7, 2012 at 17:16
  • @rontornambe I have made some edits. Please check that out. Commented Feb 7, 2012 at 17:24

2 Answers 2

3

Due to the same origin policy restriction that't built into browsers you cannot send AJAX requests to different domains. A possible workaround is to have the server return JSONP instead of JSON. Here's an example of a custom JsonpResult that you could use in your controller action.

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

Comments

1

Can U try JSONP ? Why json? It's perfect to cross-domain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.