I wrote a javaScript which will call one of the JSON API web services when the user click on a button , so i wrote the following inside a custom HTML field on the Joget form builder, but nothing will be displayed when the user click on the button and only the #title will be changed.

    <script language="JavaScript">

   function test () {
    $.ajax({
        url: "http://localhost:8080/jw/web/json/workflow/package/list",
        type: "GET",
        cache: false,
        success: function (result) {


            $('#products').empty();
            $('#title').text("All Processes");
            $.each(result.data, function (key, val) {
                var str = val.packageName;
                $('<li/>', { text: str })
                    .appendTo($('#products'));
            });


        }
    });
};
    </script>
    <form name="myForm">
      <input type="button"
             value="Display alert box"
             name="button1"
             onClick="alert('You clicked the first button.')"><br>
      <input type="button"
             value="Call on button 1"
             name="button2"
             onClick="test()">
<h1 id ="title"></h1>

<ul id="products">

</ul>

    </form>

Can anyone advice how can i make my code works well . baring in mind that i run my code inside a visual studio web project and the above code worked well and the list of packages which were returned from the APi call were displayed successfully.

Best Regards

  • No labels

4 Comments

  1. Hi BR,

    You will need to add another parameter into your AJAX call to indicate that you are expecting a plain text response.

    dataType : "text"

    Try to run the following in your Firebug

    $.ajax({
    	url: "/jw/web/json/workflow/package/list",
    	type: "POST",
    	dataType: "text",
    }).done(function(data) {
    	obj = jQuery.parseJSON(data);
    	$.each(obj.data, function (key, val) {
    		var str = val.packageName;
    		console.log(str);
    	});
    });

    Good luck!

    1. Thanks for the reply. i tried

      dataType : "text"

      but still the javascript will not display the list of processes. but i have also these two questions:-

      1. why i should define the dataType to be plain text, although the returned result from the call will be JSON?

      2. i am calling a Get service ,, why i should defined  type: "POST",

      Thanks in advance for yuor help.

      Best Regards

  2. i changed thedata type to be

    dataType : "JSONP"

    and the call worked fine.

    Best Regards

    1. Hi there,

      Glad it helps. Welcome.