ascx.cs
protected string BindData()
{
List<Product> products = product.GetRepeaterData(prod);
string json = JsonConvert.SerializeObject(products);
return json;
}
ascx
<script type="text/javascript" language="javascript">
function doSomething() {
$.ajax({
type: "POST",
url: "/ProgramListSimple.aspx/BindData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
});
};
I am not able to see any alert ..I dont know if the ajax function is doing what it is suppose to do..this code is for user control & not on the aspx page does that matter? while debugging I am able to see the serialized data in json string. Its just that its not rendering on the client side....working on it since morning now I need some help please..any examples or any doc can also be useful..
You have to use the d property
alert(msg.d);
If you are using Chrome or Firefox to debug use the following to inspect a JavaScript object:
console.log("%o", msg);
In Chrome press Ctrl + Shift + J to show the developer console
I created a new aspx page. Transfered all the code behind logic to the aspx.cs from ascx.cs. The just called the url of the aspx page from my ascx page using ajax callback
type: "GET",
url:'<%=VirtualPathUtility.ToAbsolute("~/ProgramListSimpledetail.aspx") %>',
data: dataObject,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data)
I just added a small part of the fix that is very important for this to work other then this there were bunch of things that were added to make the ascx page inherit the properties from the apsx page.... but I think that was mainly related to my code...so I hope this helps someone in future...thanks for all those who tried to contribute..
Related
I'm using Webform and Jqgrid to display master-detail information on same page.
Here is my Jqgrid definition:
Master JQGrid:
$("#MachineListGrid").jqGrid({
url: 'AdminHandler.ashx',
datatype: "json",
...
});
Detail JQGrid:
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandler.ashx',
datatype: "json",
...
});
my question is, how does ashx file identify data to return json data back to the correct jqgrid?
I'd looked at the same between aspx and ashx from this tutorial but the tutorial only gave sample one JQGrid on page.
on the code, here is the way to capture the request:
System.Collections.Specialized.NameValueCollection forms = context.Request.Form;
The way that will solve the problem is to have two url - one for the master and another for the detail
$("#MachineListGrid").jqGrid({
url: 'AdminHandlerMaster.ashx',
datatype: "json",
...
});
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandlerDatil.ashx',
datatype: "json",
...
});
If you can't do this you may identify it with additional parameter in the post data something like this
$("#MachineListGrid").jqGrid({
url: 'AdminHandler.ashx',
datatype: "json",
postData : { gridtype:"master"},
...
});
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandler.ashx',
datatype: "json",
postData : { gridtype:"detail"},
...
});
In the response you will need to get the gridtype parameter to identify the master and detail data
I am trying to call a simple hellowworld function of ASP .NET WebService using JQuery AJAX call written in separate HTML file i.e. Not aspx file in ASP .NET.
Here is code of my AJAX call which works fine in aspx based web client.
$.ajax({
type: "POST",
url: "/Test1/RSSReader.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#output").text(msg.d);
},
error: function (e) {
$("#output").html("WebSerivce error " + e.responseText);
}
});
I want to call same webservice and same method using .html file but it is returning error. I tried giving server address i.e. localhost but still no response.
When I am deploying my solution to my hoster a specific jquery element is just not showing up..
Its showing when i run it on my local pc.. but when deployed it just isnt there.. its a jquery element that renders a menu..
any ideas what could be causing this ?
I've uploaded all the references and everything..
jQuery.ajax({ type: "POST",
url: "/Bubble/MyInterests",
dataType: "json",
traditional: true,
success: function (data) {
$("#coInterestContainer").empty();
$("#coInterestTemplate").render(data).appendTo("#coInterestContainer");
$(document).on('click', '.coInterest', function () {
location.href="/Bubble/View/" + $(this).attr("id") + "?CreateOffer=1";
});
}
});
This may be a duplicate of any question but I was not able to find a solution to my problem.
I am building an MVC4 application in which I want to load JSON data from another JavaScript file, using AJAX.
For this, I tried using $.getJSON(), $.ajax(), $.get() other similar functions to load data. It is a simple AJAX call, nothing fancy! Eventually I realized that I need to make this call to a controller/action which would load the JSON and return it back. This solution worked for me.
$.ajax({
type: "GET",
url: "/controller/action", // <-- How can I refer to the JSON file directly here?
contentType: "application/json; charset=utf-8",
dataType: "application/json",
success: function(data) {
},
error: function(err) {
}
});
I want to know if there is some other way for me to load this file using JavaScript in the view itself without invoking any controller/action?
Note: The file is a .json file and not a .js file. I don't want to include the file in any <script> tag!
The problem here is that you don't have a mime type of type json specified in your web.config and IIS/IIS Express is returning a 404 error.
Try modifying your web.config like so:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
You will also need to change the dataType to json instead of application/json:
$.ajax({
type: "GET",
url: "Content/your.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function (err) {
}
});
Hope this helps.
To refer a JSON file, you can use the Url.Content() method in your Javascript :
$.ajax({
type: "GET",
url: "#Url.Content("~/Content/your.json")",
contentType: "application/json; charset=utf-8",
dataType: "application/json",
success: function (data) {
},
error: function (err) {
}
});
In this case, I have a your.json file in Content folder.
Hope it helps !
I'm using ASP.NET MVC. So I have a form on my page:
<form id="MyForm" name="MyForm" method="post" action="http://www.mysite.com">
<input id="hdnType" name="hdnType" type="hidden" />
</form>
I'm using the jQuery submit action to do some validation before the form is posted. I also need to make an AJAX call to set "hdnType" based on other values from several dropdowns that I didn't include in this example:
$('#MyForm').submit(function()
{
if (!ValidForm())
return false;
$.ajax({
type: "POST",
url: '/Home/GetType',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response)
{
$('#hdnType').val(response);
}
});
return true;
}
Everything in the submit() function is supposed to run before the form posts. This was working correctly before I added that AJAX call, but now when the code reaches my AJAX call, the form posts before I can set "hdnType".
Is there a way around this?
The ajax call has a parameter async. By default it is true. This causes execution to not be held and the function to complete. Try changing it to
$.ajax({
async:false,
type: "POST",
url: '/Home/GetType',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response)
{
$('#hdnType').val(response);
}
});
This may be a poor practice as it will freeze the browser until it is done. Consider using an asyc call back function as Dave suggests
The success function is called asynchronously, after the ajax call gets a response. If you want to set hdnType before the form is posted you'd have to move that call outside the $.ajax call. It looks like you need to get the result for hdnType from your POST action first, in a separate function, then call submit on the form.