Using with Jsonp
1. Install package by Tools > NuGet Package Manager > Package Manager Console
Install-package WebApiContrib.Formatting.Jsonp
2. Register in WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
config.Formatters.Insert(0, jsonpFormatter);
}
3. Apply JQuery Using Jsonp
function getdata() {
$.ajax({
type: "get",
url: "http://localhost:52359/api/employees_/", //cross domain
contentType: "application/json",
dataType: "jsonp",
data: JSON.stringify({
Srno: $("#Srno").val(),
Emp_Name: $("#Emp_Name").val(),
Address: $("#Address").val(),
Mobile: $("#Mobile").val()
}),
success: function (data) {
alert(data);
console.log(data);
var dt = JSON.parse(data);
//alert(jsonObj.length);
},
error: function (data) {
alert("error : " + data);
},
failed: function (data) {
alert("failed : " + data);
}
});
}
1. Install package by Tools > NuGet Package Manager > Package Manager Console
Install-package WebApiContrib.Formatting.Jsonp
2. Register in WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
config.Formatters.Insert(0, jsonpFormatter);
}
3. Apply JQuery Using Jsonp
function getdata() {
$.ajax({
type: "get",
url: "http://localhost:52359/api/employees_/", //cross domain
contentType: "application/json",
dataType: "jsonp",
data: JSON.stringify({
Srno: $("#Srno").val(),
Emp_Name: $("#Emp_Name").val(),
Address: $("#Address").val(),
Mobile: $("#Mobile").val()
}),
success: function (data) {
alert(data);
console.log(data);
var dt = JSON.parse(data);
//alert(jsonObj.length);
},
error: function (data) {
alert("error : " + data);
},
failed: function (data) {
alert("failed : " + data);
}
});
}
No comments:
Post a Comment