javascript - Retrieving a JSON.parse() string from a server -


i start saying learning how program in jquery/javascript, , running issue using json.parse(). understand format, , why people use it... have not been able work in of code projects.

i have read in books/online on here in how use it, think read on it. confused , second guessing know it.

with said, jquery/javascript class taking asking me use assignment, through ajax using mamp/localhost server.

the 2 codes below section need fill in //todo information. 1 javascript (client-side), other php (server-side). think i've set other //todo information correctly, keep getting token error json part.

i looked on here solution, again, think i've confused myself badly , need help. appreciate feedback, insight, or information.

-javascript-

var calculatempg = function () {     // these lines commented out since server perform these checks     // if (!checknumber("miles") || !checknumber("gallons")) {         // return;     // }     var miles = $("#miles").val();     var gallons = $("#gallons").val();     console.log("ajax request issued.");     var result;     $.ajax({         url: "service.php?action=calculatempg&miles="+miles+"&gallons="+gallons,          cache: false,         datatype: "text",         success: function(msg) {             console.log("ajax response received.");         //  todo: parse json string returned server (see json.parse())         json.parse("result");         if (result.status === 'success') {         //  todo: mpg value returned server , display user.             $("#mpg").val($_get("result"));             console.log("json working!");         }         else {             // todo: name of variable error. hint: @ 'fail' result service.php              $_get[fail(id)];             // todo: report error user using invalidnumber() function.             alert("{status: 'failure', variable: <variable name>}");         }     } }); };  $(document).ready( function () {     $("#miles").blur(function () {         checknumber("miles");     });     $("#gallons").blur(function() {         checknumber("gallons");     });     $("#calculate").click(calculatempg);     $("#miles").focus(); }); 

-php-

<?php if ($_get) {     if ($_get['action'] == 'calculatempg') {         $miles = htmlspecialchars($_get['miles']);         $gallons = htmlspecialchars($_get['gallons']);         // validate miles         if (strlen($miles) == 0) {             fail("miles");         }         $miles_chars = str_split($miles);         ($i=0; $i< count($miles_chars); $i++) {             if ($miles_chars[$i] < "0" || $miles_chars[$i] > "9") {                 //error_log("miles_chars check failed at: " + $i);                 fail("miles");             }         }         // validate gallons         if (strlen($gallons) == 0) {             fail("gallons");         }         $gallons_chars = str_split($gallons);         ($i=0; $i< count($gallons_chars); $i++) {             if ($gallons_chars[$i] < "0" || $gallons_chars[$i] > "9") {                 fail("gallons");             }         }         // validate $miles , $gallons calling $fail along way         $result = $miles/$gallons;         if ($result) {             success($result);         } else {             fail("mpg");         }         exit ;     }  }  function fail($variable) {     die(json_encode(array('status' => 'fail', 'variable' => $variable))); }  function success($message) {     die(json_encode(array('status' => 'success', 'message' => $message))); } 

edited additional 1 have made changes json information in regard 'var result' (thanks several of responses here). i'm starting understand json bit better.

another question have (now) how isolate part of json message whole being transmitted?

a piece of 'json.parse(msg)' returned include answer equation miles/gallons, don't know how to... extract json.

the solution equation miles/gallons appears in 'msg' output.

thanks.

edited additional 2 question has been solved! while perusing around stackoverflow solution question in previous edited section, found answer here: json response parsing in javascript key/value pair.

the answer this: under //todo section asking mpg value, put following code - $("#mpg").val(result.message); - says in json section of variable result, take part of json marked 'message', value being equation solution.

thank responded solutions problem. appreciate fast responses, great suggestions, , information in understanding json.

-ecp03

json.parse() requires send valid json string.

"result" not valid json string. in success function have defined parameter msg - contain? try console.log(msg) @ beginning of success function , @ console output.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -