Arun D
Arun D

Reputation: 279

Parameter can't pass using ajax in Cross Domain

I have used cross domain to call one domain to another as below ,

My ajax call from mvc project as done below,

var tableparameter = '{TableName: "' + $("#tableOneTableName").val() + '" }';
$.ajax({
            type: "POST",
            url: "http://localhost:55016/api/ajaxapi/onchangetableone",
            data: tableparameter,
            success: function (response) {
               alert("hi");
            }
});

Then i added below code in web.config file within web api project as below,

<httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>

Then i written action as below,

[HttpPost]
[Route("api/ajaxapi/onchangetableone")]
public List<DropDownListValuesForQuery> OnChangeTableOne(TableNameFormCustomFilters TableParameter)
{
        CaseModel query = new CaseModel();
        List<DropDownListValuesForQuery> listValues = new List<DropDownListValuesForQuery>();
        listValues = query.GetColumnNames(TableParameter.TableName);
        return listValues;
}

And list values as below,

public class TableNameFormCustomFilters
{
        public int TableName { get; set; }
}

Here i can access the webapi source using cross domain but the parameter value not come from script. Please give your suggestion.

Thanks in advance.....

Upvotes: 2

Views: 179

Answers (2)

Asif Raza
Asif Raza

Reputation: 1021

change your javascript obj little bit ,

    var TableParameter =
                {
                    TableName: $("#tableOneTableName").val()
                };

Ajax

$.ajax({
        type: "POST",
        url: "http://localhost:55016/api/ajaxapi/onchangetableone",
        data: TableParameter ,
        success: function (response) {
           alert("hi");
        }
  });

' TableParameter ' must be same as model parameter

Upvotes: 1

Laxman Gite
Laxman Gite

Reputation: 2318

Check below code :

var TableParameter = { "TableName": $("#tableOneTableName").val() }; 

$.ajax(
{
url: 'http://localhost:55016/api/ajaxapi/onchangetableone/',
     type: "POST",
     async: false,
     contentType: "application/json;",
     data: JSON.stringify(TableParameter),
     success: function (result) {
     alert("hi");
 }
});

Cheers !!

Upvotes: 1

Related Questions