Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: JSON Return Values

by NetWallah (Canon)
on Mar 21, 2018 at 04:56 UTC ( [id://1211392]=note: print w/replies, xml ) Need Help??


in reply to JSON Return Values

Your JavaScript looks valid. However, according to the documentation at https://api.jquery.com/jQuery.getJSON/, getJSON can fail silently it it encounters JSON errors.

This article on StackOverflow (and the documentation above) point to a more-error-checked call, using the .fail and .done syntax.

                Python is a racist language what with it's dependence on white space!

Replies are listed 'Best First'.
Re^2: JSON Return Values
by tultalk (Monk) on Mar 21, 2018 at 09:43 UTC

    Thanks.

    I assumed no SON errors because I was able to capture the data in an IFrame:

    var data = {"DD":"2019-01-01","DP":"0000-00-00","MD":"120.00","M +J":"2018-01-01","address1":"1327 North St","address2":"Ste 242","busi +ness":null,"city":"Deluth","comments":"This is an insert test with ac +tion converted to sub calls","email":"tj@resolve.net","forename":"Tre +vor","id":65,"lastname":"Johnson","password":"xmk1041","phone_cell":" +(517) 269-1014","phone_home":"(414) 956-9219","pin":"TbwmJ","position +":"General Member","state":"MN","user_id":65,"username":"bwm65","zip" +:"36925-6647"}; loadJSONFormData(data);

    and load in to form fields using loadJSONFormData(data). <.p>

    So as I said, I assumed no error in the JSON data

    I will explore further

      And checking the console.log reveals:

      Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.xxx.org/httpsdocs/cgi-bin/update_tables.cgi?action=updatetable_167&kind=0&searchterm=19. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

        I added this to Firefox and works fine

        https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

        Thanks. Now to get back to the main issue of loading the form fields

        Gad! What a diversion

Re^2: JSON Return Values
by tultalk (Monk) on Mar 22, 2018 at 14:58 UTC

    I would appreciate some insight here:

    I am used to drop downs being bound to database fields

    I have a select drop down on a form used to populate the insert/update query sent to the server.

    <td><select required id="position" name="position" onChange="changeFla +g(1)" > <option value="0">Select <option value="1">Board Member <option value="2">Administration <option value="3">General Member </select></td>

    When I repopulate the form field with JSON

    $('form').loadJSON(data);

    , I expect the data from the database field to be displayed. All the other form input fields are populated correctly.

    By the way, this: onChange="changeFlag(1) is not calling anything. A dummy

    In reading:

    Populating form element Form is populated with JSON object using the following JavaScript call: $('form').loadJSON(data); This call matches object properties with the form elements by name and depending on the type of the form element populates value. Following rules are used while the form elements are populated:

    Multiple selection lists

    When plugin matches property with multiple selection list element, it expect that property is array of values that should be selected in the list. Plugin selects all values in the multi selection list that exist in the property of the JavaScript object.

    Assuming that a "select" control comes under the definition of "Multiple selection lists", it sounds as if it would work and display the applicable JSON content.

    I set up a test with a select where I use a button to change the "value" of the select index and that works fine.

    Evidently, $('form').loadJSON(data), does not address the indexing as implied above.

      Where in your question does Perl come into play?

      I think your question is mostly about jQuery, which is off-topic here. You should first check that sending the appropriate (hard-coded) JSON works. Then you can debug your Perl code until it creates the same JSON.

      If you don't know what the correct JSON to create is, no fudging on the Perl side of the world will help you. You will need jQuery expertise, which certainly can be found. But this is not the forum for jQuery expertise.

        You are so correct. Forgot where I was. Perl on one side HTML on the other, JQuery in the middle.

      The data back from JSON includes "position":"3" so the information is there.

      The javascript looks good:

      (function ($) { $.fn.loadJSON = function (obj, options) { function setElementValue(element, value, name) { var type = element.type || element.tagName; if (type == null) return; type = type.toLowerCase(); switch (type) { case 'radio': if (value.toString().toLowerCase() == element.valu +e.toLowerCase()) $(element).attr("checked", "checked"); break; case 'checkbox': if (value) $(element).attr("checked", "checked"); break; case 'select-multiple': var values = value.constructor == Array ? value : +[value]; for (var i = 0; i < element.options.length; i++) { for (var j = 0; j < values.length; j++) { element.options[i].selected |= element.opt +ions[i].value == values[j]; } } break; case 'select': case 'select-one': case 'text': case 'hidden': $(element).attr("value", value); break;

      At a loss. I don't see "value as either a global attribute or an attribute of select.

      Attributes defines: value <button>, <input>,

    • , <option>, <meter>, <progress>, <param>

      So it is an attribute of option not select

        Resolved by using a more recent script as suggested on jquery forum.

        Thanks>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found