0

I'm using web service and trying to pass a json object and my ajax in the fron end doesn't receive it. It says it was a success but there is nothing in the response object. I've tried everything I can think of and still nothing.

Web Service

 rt = "{'RegistrationTypeID':'" + regType.RegistrationTypeID + "'}";
        string json = JsonConvert.SerializeObject(rt);
        return json;

Javascript

$.ajax({
    type: "POST",
    url: "/static/svc/RegistrationForm.asmx/GetRegistrationType",
    timeout: 0,
    success: function (response, status) {
        console.log("status: " + status);
        
        var types = response;
        console.log("types: " + types);

        $.each(types, function (index, value) {
            console.log("In loop");
            $('select').append('<option value="' + value.RegistrationTypeID + '">' + 
value.RegistrationTypeName + '</option>');

        });

    },
error: function (response) {
    console.log(response),
        console.log(response.statusCode);
    console.log(response.statusText);
        
}

});
NewbieCoder
  • 75
  • 1
  • 8
  • do you get anything if you call the ajax url directly in browser? – caramba Aug 14 '20 at 20:18
  • No I don't get anything – NewbieCoder Aug 14 '20 at 20:23
  • That means the problem has nothing to do with ajax at the moment. You should first make sure you can get a response the simplest way possible. When you have the response (let's assume it is a json string) then you try to get it with an ajax call – caramba Aug 14 '20 at 20:25
  • JSON requires double quotes so what you are manually creating won't be valid. Generally a bad idea manually creating it when you can use built in serializers in whatever language you need – charlietfl Aug 14 '20 at 20:25
  • Okay I changed it to this. rt = "{ \"RegistrationTypeID\" : \"" + regType.RegistrationTypeID + "\" }"; and still nothing – NewbieCoder Aug 14 '20 at 20:28
  • does this answer might help you: https://stackoverflow.com/a/48905307/2008111 – caramba Aug 14 '20 at 20:31
  • Considering you've got a string already, you don't have to serialize anything - simple `return "{'RegistrationTypeID':'" + regType.RegistrationTypeID + "'}";` in your web service should be enough. But, as @charlietfl suggests, building string by hand isn't a preferable way to do such things. – Marek Piotrowski Aug 14 '20 at 20:32
  • voting to close this question. @arivera618 please feel free to update the question or create a new one. Also please do update the tags! as it stands now it is not a javascript/ajax problem – caramba Aug 14 '20 at 20:39
  • you don't need the "timeout: 0" delete it and try agen, but i am not sure if work. – Jaber Alshami Aug 14 '20 at 20:39
  • I tried that code and still nothing. – NewbieCoder Aug 14 '20 at 20:43
  • @MarekPiotrowski Those quotes are backwards which is why it won't be valid JSON – charlietfl Aug 14 '20 at 20:54
  • @charlietfl content type is not specified at all for the ajax. Not sure if it's even validated? I suggest running your backend service in DEBUG to see what happens there. Hard to judge as you didn't include any error messages. As caramba suggests, if you can't see anything when you manually type URL in the browser, service does not work. – Marek Piotrowski Aug 14 '20 at 21:03

0 Answers0