Reputation: 129
I am attempting to send an AJAX request from an ASP.NET Gridview row. Each row in the gridview shows completed jobs. Some jobs have files that can be downloaded. The end goal here is to show the download urls in a colorbox overlay, but I am having trouble just returning the list of files to the base page now. To show that there are files to download from an order I unhide a button control in the order number column. Javascript on the page follows.
$(document).ready(function () {
$("#<%=gvMessenger.ClientID%> input").filter(":not(:has(table, th))").click(function (e) {
e.preventDefault();
var $cell = $(e.target).closest("td");
//$("#<%=gvMessenger.ClientID%> td").removeClass("highlight"); $cell.addClass("highlight"); $("#message").text('You have selected: ' + $cell.text());
$("#Message").html("");
var orderID = $cell.text()
orderID = $.trim(orderID);
sendData(orderID);
function sendData(orderID) {
var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ?
loc + "CompletedOrdersNew.aspx" : loc;
$.ajax({
type: "POST",
url: loc + "/GetFiles",
data: "{'orderID':'" + orderID + "'}",
contentType: "application/jason; charset=utf-8",
datatype: "json",
success: function (msg) {
$("ContentArea").html(msg.d);
},
error: function () {
alert("An error occured in getting your files.");
}
});
}
});
});
The function in the page codebehind that should fire on the ajax request follows.
<WebMethod()> _
Public Shared Function GetFiles(ByRef orderID As Integer) As String
Dim dict As New Dictionary(Of String, Object)
Dim dt As DataTable
dt = Dac.ExecuteDataTable("GetS3Files", Parameter("@OrderID", orderID))
Dim arrr(dt.Rows.Count) As Object
For i As Integer = 0 To dt.Rows.Count - 1
arrr(i) = dt.Rows(i).ItemArray
Next
Dim json As New JavaScriptSerializer
Return json.Serialize(dict)
End Function
When watching the page in FireBug I see the request go out to the GetFiles function with the orderID number {'orderID':'10000315'}
POST http://localhost:57210/AMSSite/Customer/CompletedOrdersNew.aspx/GetFiles
But the call does not fire the GetFiles function and the response I am getting is the page html.
Not sure what is going on here.
Upvotes: 0
Views: 309
Reputation: 129
I'll answer my question for the sake of any other VB.NET/ASP.NET folks running into this problem. Aside from my misspelling the issue here was that my WebMethod function was ByRef. It needs to be BYVAL. After banging my head against the wall and reading some great stuff on javascript and json services I found the real answer at http://projectsmm.com/technet/asp/index.shtml. VB.Net by default sets function variables to ByRef. For some reason .NET web services don't accept this. Change to ByVal and it works.
Hope this helps some one else not spend the hours I looked at this.
Upvotes: 0
Reputation: 22448
You made a typo in contentType
option. Should be: contentType: "application/json; charset=utf-8"
Upvotes: 1
Reputation: 97672
Try sending orderID
as a number since you webmethod expects an int, also in JSON keys and strings are quoted with double quotes("
)
data: "{\"orderID\":" + orderID + "}",
Upvotes: 1