json being returned via url but not via jquery


json being returned via url but not via jquery



I'm using Visual Studio to develop an MVC app and I have a jquery function that seems to be causing problems. I developed an action method that returns a json object (jquery calls action > action calls a service > the service returns an object > action converts object to a model via a map then passes the model to json). The action can be called via url (passing an id parameter via the query string) and it returns the json to the web page, however, when I call the jquery function via the web page (click on a button that executes the jquery), I get a "localhost:50216 says an error has occurred" before the rest of the jquery function executes. Been researching this for several days and tried using Chrome's debugger to no avail. Hopefully this makes sense, but if I need to provide further information please let me know. Any insight would be much appreciated.
Best,
Jon


$("#delayEditModal")
.on("show.bs.modal",
function (event) {
var button = $(event.relatedTarget);
var delId = button.data("guid");
var name = button.data("name");
var modal = $(this);
$.get("@Url.Action("GetDelayDataForEditing", "TrainActivity")", function(data){
alert("Data: " + data);
});
modal.find(".modal-body").text("Edit the Delay at " + name + " with id " + delId);
modal.find(".modal-footer #delayEditButton").data("guid", delId);
});



I'm able to get to the popup (when I click on the button that has the id=#delayEditModal) that displays "Edit the Delay at...", however before that - I believe when the $.get method executes - I get the 500 error. Also, I never hit the alert("Data: "..) portion.



So I've looked at a couple articles (one of them Url.Action: How to Pass Parameter from View to Controller?) andmade some changes to my code. Still no success. Will continue to research solutions.


$("#delayEditModal")
.on("show.bs.modal",
function (event) {
var button = $(event.relatedTarget);
var delId = button.data("guid");
var name = button.data("name");
var modal = $(this);
$.ajax({
type: "GET",
url: "@Url.Action("GetDelayDataForEditing")/" + delId,
//data: delId,
success: function () {
alert("Success");
//$('#delays-grid').data('kendoGrid').dataSource.read();
//$("#delayAddModal").modal("hide");
},
error: function () { alert("error"); }
});
//modal.find(".modal-body").text("Edit the Delay at " + name + " with id " + delId);
modal.find(".modal-footer #delayEditButton").data("guid", delId);
});



Edit: I don't think I'm even hitting my ActionResult method. I'v set a break point there and don't ever hit it.



Edit: I'm finally able to reach "Success":


$("#delayEditModal")
.on("show.bs.modal",
function (event) {
var button = $(event.relatedTarget);
var delId = button.data("guid");
var name = button.data("name");
var modal = $(this);

$.ajax({
type: "GET",
url: "@Url.Action("GetDelayDataForEditing")/" + "?delayId=" + delId,
dataType: 'json',
//data: delId,
success: function (data) {
alert("Success" + data);
//$('#delays-grid').data('kendoGrid').dataSource.read();
//$("#delayAddModal").modal("hide");
},
error: function () { alert("error"); }
});
modal.find(".modal-body").text("Edit the Delay at " + name + " with id " + delId);
modal.find(".modal-footer #delayEditButton").data("guid", delId);
});



However, I'd like to figure out how to display the raw contents of the json object in the modal. This will have to be a task for Monday. Many thanks to all for sharing your advice and insight. Have a great weekend!





@Taplar sorry I forgot to include the code... been a long week.
– BeginnerOne
Jun 29 at 18:31





if you're getting a 500 error then it's a server-side error...so you need to check your server's event or exception logs to get details of what went wrong. One thing though - in the question text you mention that "The action can be called via url (passing an id parameter via the query string)"...yet in your $.get call to it you don't provide any such parameter. Could that be the problem?
– ADyson
Jun 29 at 19:12






@ADyson Thank you for your prompt response. I've already checked my localhost server logs and there is nothing there except the generic 500 substatus: 0. Also, your observation about the lack of a parameter is one that I've wondered myself. Sadly, I've been unable to get a straight answer from any of the senior developers here - or if they HAVE given me an answer it's in Greek. SMH.
– BeginnerOne
Jun 29 at 19:20





@ADyson Okay, so my supervisor finally gave me a straight answer and confirmed my suspicion that the ID was missing from the jquery call to the action. I will have to research how to do that. Thank you for providing the impetus for me to "balls up" and approach my lead about that. :)
– BeginnerOne
Jun 29 at 19:44





Can you show us the definition of the action method on the server please. Also try looking for errors in the Windows Event Viewer on the server rather than the IIS logs. And make sure your code isn't swallowing exceptions anywhere (e.g. using try-catch blocks which simply ignore the caught exception rather than logging and/or re-throwing it.
– ADyson
Jun 29 at 21:41




1 Answer
1



In the question text you mention that "The action can be called via url (passing an id parameter via the query string)"...yet in your $.get call to it you don't provide any such parameter.



You need to provide a parameter to the server, with the same name as the one your action method is expecting, for example:


url: "@Url.Action("GetDelayDataForEditing")/" + "?delayId=" + delId,



The part after the ? is the parameter name, then the part after the = is the value to send for that parameter.


?


=






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift