Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:44:54+00:00 2026-05-25T20:44:54+00:00

I create a simple MVC Controller action, that takes some json data – then

  • 0

I create a simple MVC Controller action, that takes some json data – then return true or false.

    [AllowCrossSiteJson]
    public JsonResult AddPerson(Person person)
    {
            //do stuff with person object
           return Json(true);
    }

I call it from javascript:

        function saveData(person) {
            var json = $.toJSON(person); //converts person object to json
            $.ajax({
                url: "http://somedomain.com/Ajax/AddPerson",
                type: 'POST',
                dataType: 'json',
                data: json,
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    alert("ok");
                }
            });
        }

Everything works as long as I am on the same domain, but as soon as I call it from another domain, I run into problems.

On the controller is an action filter “AllowCrossSiteJson” that sets the header “Access-Control-Allow-Origin” to “*”, allowing any origin to access the controller action.

public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
        base.OnActionExecuting(filterContext);
    }
}

However – I then get this error in firebug, when calling across domains:

OPTIONS http://somedomain.com/Ajax/AddPerson?packageId=3 500 (Internal Server Error)
XMLHttpRequest cannot load http://somedomain.com/Ajax/AddPerson. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.

What is wrong here?

I have been looking through possible solutions for hours, and it seems to be something to do with jquery using OPTIONS (not POST as I would expect).

If that is indeed the problem, how can I fix that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:44:55+00:00Added an answer on May 25, 2026 at 8:44 pm

    I’d recommend you JSONP, it’s the only really cross browser and reliable solution for cross domain AJAX. So you could start by writing a custom action result that will wrap the JSON response with a callback:

    public class JsonpResult : ActionResult
    {
        private readonly object _obj;
    
        public JsonpResult(object obj)
        {
            _obj = obj;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var serializer = new JavaScriptSerializer();
            var callbackname = context.HttpContext.Request["callback"];
            var jsonp = string.Format("{0}({1})", callbackname, serializer.Serialize(_obj));
            var response = context.HttpContext.Response;
            response.ContentType = "application/json";
            response.Write(jsonp);
        }
    }
    

    and then:

    public ActionResult AddPerson(Person person)
    {
        return new JsonpResult(true);
    }
    

    and finally perform the cross domain AJAX call:

    $.ajax({
        url: 'http://somedomain.com/Ajax/AddPerson',
        jsonp: 'callback',
        dataType: 'jsonp',
        data: { firstName: 'john', lastName: 'smith' },
        success: function (result) {
            alert(result);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple CMS for my asp.net-mvc site. Will I save
I'm playing with the ASP.NET MVC Framework, trying to create a simple site. My
I recently discovered that I can use lambdas to create simple event handlers. I
I recently did some refactoring of my mvc application and realized that there are
I have a controller that extends Zend_Controller_Action. It contains some actions that I need
I have a simple mvc project consisting of one table named Persons and that
ASP.NET MVC makes it really easy to create editing templates for simple, flat objects.
I'm trying to write a test for an ASP.Net MVC controller action. I'd like
I'm new to ASP.NET MVC and I'm trying to create a very simple blog
I created a simple ASP.NET MVC version 1.0 application. I have a ProductController which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.