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

I have the following 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, for example: http: // localhost: 48610 / Person / GetPerson /? Id = 6 . Why is there a question mark? I get a server error . The parameter dictionary contains a null entry for the "id" parameter of a non-nullable type of "System.Int32" ....

If I make a request manually without a question mark, it works great.

+3
source share
6 answers

URL- "?". MVC ( -), . , :

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

, , MVC. Ex, :

    public function GetPerson(string id, string type) {
         // your code
    }

:

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

getJSON GET URL-, . $.post.

 $.post('/Person/GetPerson/', { id: $('#id').val() }, function(data) {
        $('#collar').text(data.collarNumber);
        $('#name').text(data.Name);
        $('#email').text(data.EmailAddress);
    }, 'json');

, /Person/GetPerson/123, URL- { } .

+2

GET .

, URL- ( )

+1

, getJSON, querystring, . URL?

+1

getJSON. :

 $.getJSON('/Person/GetPerson/'+userID,, function(data) {
        });
+1

GET / , data, . jQuery ?, /.

Try passing nullas a second parameter if you do not want to send any data.

http://api.jquery.com/jQuery.getJSON/

+1
source

Source: https://habr.com/ru/post/1744754/


All Articles