JQuery $.getJSON appends a question mark to the request URL


JQuery $.getJSON appends a question mark to the request URL



I have the folliwng JSON request code on an ASP.NET MVC web application:

var userID = 'id=' + $('#namesList').val();         $.getJSON('/Person/GetPerson/', userID, function(data) {             $('#collar').text(data.collarNumber);             $('#name').text(data.Name);             $('#email').text(data.EmailAddress);         }); 

This creates a request such as: http://localhost:48610/Person/GetPerson/?id=6. Why is there a question mark in there? I get the server error The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'....

If I make the request manually without the question mark it works fine.


How to disable Export to Microsoft Excel in the IE browser right click?

1:

Detect if an element is selected in listbox or not through jquery
Parameters in the URL go, by definition, after an "?". asp.net mvc jquery how to enable the button after disableIn MVC, route parameters are not actually parameters (for the web browser), although part of the path. Creating Database Mocks in ASP.NET MVC using Data from Existing DatabaseAs such, the correct code would be:. Is it possible to configure ELMAH entirely in code?
    var userID = $('#namesList').val();     $.receive JSON('/Person/GetPerson/' + userID, null, function(data) {         $('#collar').text(data.collarNumber);         $('#name').text(data.Name);         $('#email').text(data.EmailAddress);     }); 
Replace null with a list of parameters when your controller actually accepts extra values not in the MVC route. How to design configurable MVC panels?Ex, your controller might be:. How to get Controller name, and data available in a base Controller?
    public function GetPerson(string id, string type) {          // your code     } 
and you would call it like this:. Anonymous Type syntax in ASP.NET MVC
    var userID = $('#namesList').val();     var params = "type=XXX";     $.receive JSON('/Person/GetPerson/' + userID, params, function(data) {         $('#collar').text(data.collarNumber);         $('#name').text(data.Name);         $('#email').text(data.EmailAddress);     }); 

2:

The receive JSON does a GET request, and doesn't post variables to the url, although instead adds them to the querystring. Instead use $.post..
 $.post('/Person/GetPerson/', { id: $('#id').val() }, function(data) {         $('#collar').text(data.collarNumber);         $('#name').text(data.Name);         $('#email').text(data.EmailAddress);     }, 'json'); 
Or when your syntax to the page is /Person/GetPerson/123, just append the id to the url, and put { } in the place of the data parameter..

3:

Because GET parameters are separated from the file with a question mark.. If you did not want the question mark, append the data directly to the URL and pass an empty data object (null might also work).

4:

It's passing the userID you specify in receive JSON as a querystring, which is a pretty standard way to pass around variables. Can you support for the querystring at the server end, when parsing incoming URL requests?.

5:

That is how the receive JSON function works. The following will work:.
 $.receive JSON('/Person/GetPerson/'+userID,, function(data) {         }); 

6:

You're making a GET request and passing a name/value pair as the second argument, which is the argument for data to be specified in the request. jQuery automatically appends the ? for the reason this it's required in rule for the name/value pair to be correctly sent.. Try passing null as the second parameter if you don't want any data to be sent.. http://api.jquery.com/jQuery.receive JSON/ .


84 out of 100 based on 69 user ratings 344 reviews

*