3 Replies Latest reply on Oct 27, 2017 7:29 PM by skumar12

    Jquery Ajax to call API controller Issue

    mamyrand

      Hello Everyone,

      Can someone help me please?

      I have created my very first ASP.net project and intend to invoke PI Timestamp and value to text boxes using PIwebAPI.

      My attachment includes:

      the aspx file - the Jquery Ajax Call - The routing configuration - and the API Controller.

      remark:

      Browsing directly using the API url: http://localhost:49499/PI/PI returns:

      {"Timestamp":"08/04/2017 07:20:00","Value":"8258.106"}

      which are exactly the Json content that I am expecting.

      But I fail when I try to use Jquery Ajax to invoke these 2 parameters and try to parse it into my text boxes.

      I get error

      Any help is very appreciated.

       

      Mamy

        • Re: Jquery Ajax to call API controller Issue
          pmartin

          Hi Mamy,

           

          You may want to try making your Action "PI" in the PIController return a JsonResult instead of a Task<JObject>.  The conversion from Object to JsonResult can be done using the Json method.  I'm not positive this will fix the issue entirely but it's definitely a step in the right direction.

           

          Also, you may wish to consider switching your ajax success method to done - success had been deprecated.  It won't matter right now but the success call may be removed in the future so better to get into the habit of using done now .

            • Re: Jquery Ajax to call API controller Issue
              mamyrand

              Hello Martin

              Thank you for your feedback, you are the first person to help me on this.

              But would you mind to provide the code that can fix this issue?

               

              Also, when you say, making your PI controller to return JsonResult instead of JObject,

              I'm a bit confused Martin, thinking that JObject is a Json Object, and my PI controller return effectively that result, it is {"Timestamp":"08/04/2017 07:20:00","Value":"8258.106"}. through  http://localhost:49499/PI/PI

               

              My main issue is that I need to consume that resource through Ajax call, take the values of Timestamp and value to feed their respective text boxes.

              Thank you.

              Mamy

            • Re: Jquery Ajax to call API controller Issue
              skumar12

              Hello Mamy, you are trying to pass parameters with jQuery ajax method and thats where you are getting error. You should see the console window in chrome browser for any error which you are getting. That will help you to debug your code.

              In jQuery AJAX method the data parameter is used to pass the values to server code like this:

                $.ajax({

              type: "POST",

              url: "jquery-ajax-subscribe.php",

              data: {email: $("#email").val()},

              success: function (msg) {

                //

              },

              error: function (req, status, error) {

                //

              }

              });