1) Add json file in your solution. like sample.json
2) Create a function in jQuery
function ReadJsonFile()
{
var obj = {};
obj.fileName = '<%=ResolveUrl("sample.json") %>';
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
debugger;
var array = $.parseJSON(response.d);
// $('[id*=Name]').val(array[0].Name);
//$('[id*=age]').val(array[0].Age);
},
failure: function (response) {
alert('hello');
alert(response.d);
},
error: function (response) {
alert('hru');
alert(response.d);
}
});
}
3) [WebMethod]
public static string GetData(string fileName)
{
using (StreamReader sr = File.OpenText(HttpContext.Current.Server.MapPath(fileName)))
{
string json = sr.ReadToEnd();
return json;
}
}
4) Set the limit of json in web.config file if json file contains the binary data
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
2) Create a function in jQuery
function ReadJsonFile()
{
var obj = {};
obj.fileName = '<%=ResolveUrl("sample.json") %>';
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
debugger;
var array = $.parseJSON(response.d);
// $('[id*=Name]').val(array[0].Name);
//$('[id*=age]').val(array[0].Age);
},
failure: function (response) {
alert('hello');
alert(response.d);
},
error: function (response) {
alert('hru');
alert(response.d);
}
});
}
3) [WebMethod]
public static string GetData(string fileName)
{
using (StreamReader sr = File.OpenText(HttpContext.Current.Server.MapPath(fileName)))
{
string json = sr.ReadToEnd();
return json;
}
}
4) Set the limit of json in web.config file if json file contains the binary data
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
No comments:
Post a Comment