locked
AJAX Errors RRS feed

  • Question

  • User1231829591 posted

    Hi all, I get 2 different sets of errors depending on the format of the data parameter of AJAX. Please take a look at my code below to see if you can spot my mistakes.

            function GetClosestStores(userdata, callback) {
                var _lat = userdata.lat();//getting latitude from google geocode
                var _lng = userdata.lng();//get longitude from google geocode
                var _distance = document.getElementById("Distance").value;
                var _results = document.getElementById("Results").value;
                var jdata = { lat: _lat, lng: _lng, distance: _distance, results: _results };
    
                $.ajax({                
                    type: "Get",
                    contentType: "application/json; charset=utf-8",
                    url: "http://localhost:21311/SearchService.asmx/GetClosestPlace",
                    data: JSON.stringify(jdata),
                    //data:jdata,
                    dataType: "json",
                    success: function (data) {
    
                        alert("Successfully register");
                        callback(data);
                    }
    
                });
            }

    If I stringify data the I get the error "status of 500 (Internal Server Error)" in Chrome and when I click on the error I see

    System.InvalidOperationException: Missing parameter: lat.

    If I do not use stringify I also get the error "status of 500 (Internal Server Error)". but when I click on the error I actually see data from the database in  json format.

    Please help point out what the cause of the errors is, thanks in advance.

    Thursday, September 17, 2015 6:14 AM

Answers

  • User-474980206 posted

    You need to use your code like this..

    User single quotes before and last of the variable name like 'lat': _lat

    Full line code

    var jdata = { 'lat': _lat, 'lng': _lng, 'distance': _distance, 'results': _results };

    this is not correct. you only need to use quotes on property names when there are not legal javascript identifiers. say you want to use {"my lat": _lat }.

    are you sure the the web service supports JSON? remove the stringify and the content-type and see a standard form post works. your web service may require a SOAP xml.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 11:18 AM

All replies

  • User603616845 posted

    Hi,

    You need to use your code like this..

    User single quotes before and last of the variable name like 'lat': _lat

    Full line code

    var jdata = { 'lat': _lat, 'lng': _lng, 'distance': _distance, 'results': _results };

    Hope this will help you.

    thanks

    Thursday, September 17, 2015 6:22 AM
  • User-474980206 posted

    You need to use your code like this..

    User single quotes before and last of the variable name like 'lat': _lat

    Full line code

    var jdata = { 'lat': _lat, 'lng': _lng, 'distance': _distance, 'results': _results };

    this is not correct. you only need to use quotes on property names when there are not legal javascript identifiers. say you want to use {"my lat": _lat }.

    are you sure the the web service supports JSON? remove the stringify and the content-type and see a standard form post works. your web service may require a SOAP xml.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 11:18 AM
  • User1231829591 posted

    It turns out I did not need the line

     contentType: "application/json; charset=utf-8",
    

    Everything works fine now.

    Friday, September 18, 2015 3:07 PM