Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I get the JSON.parse to be evaluated in and AJAX function because TextBox5-9 are empty.

This should work.

I have returning data for both differences instances from the WebMethod function getDate.
JavaScript
$("#<%= TextBox4.ClientID %>").val(result.d);
prints out either of the two JSON data below and is working.
"[{id:1, datetime:04/10/2017, col1:1, col2:2, col3:2}]"

and
"{\"id\":1,\"datetime\":04/10/2017,\"col1\":1,\"col2\":2,\"col3\":2}]"

TextBox5-9 are not giving any data like they should. The JSON class structure is not being read.

This should work.
JavaScript
<script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%= Button1.ClientID %>").click(function () {
                var mo1 = $("#<%= TextBox1.ClientID %>").val();
                var dy1 = $("#<%= TextBox2.ClientID %>").val();
                var yr1 = $("#<%= TextBox3.ClientID %>").val();
                var data = { mo:mo1, dy:dy1, yr:yr1 };
                var json1 = JSON.stringify(data);
                
                $.ajax
                ({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: json1,
                    url: "Default.aspx/GetDate",
                    success: function (result) {
                        $("#<%= TextBox4.ClientID %>").val(result.d);
                        var datereturn = JSON.parse(result.d);
                        $("#<%= TextBox5.ClientID %>").val(datereturn.id);
                        $("#<%= TextBox6.ClientID %>").val(datereturn.datetime);
                        $("#<%= TextBox7.ClientID %>").val(datereturn.col1);
                        $("#<%= TextBox8.ClientID %>").val(datereturn.col2);
                        $("#<%= TextBox9.ClientID %>").val(datereturn.col3);

                    },
                    error: function (status, ex) {
                        alert("Error Code: Status: " + status + " Ex: " + ex);
                    }
                });
                return false;
            });
        });
    </script>


What I have tried:

I have tried many returning formats of the JSON returning from the WebMethod GetDate. Like I said this produces one of the two JSONs in TextBox4 in correct form. It just does nt print out the Json Classes. I have tried "return" instead of "return.d".
Posted
Updated 12-Apr-17 17:17pm
Comments
teledexterus 12-Apr-17 15:45pm    
The Json data formats have to be changed in the WebMethod GetDate to get either form.
Bryian Tan 12-Apr-17 16:15pm    
The datetime is a string in the JSON context, should be wrapped in quote \"04/10/2017\"

it should be
string json = "[{id:1,datetime:\"04/10/2017\",col1:1,col2:2,col3:2}]";


Since the json is in array format, the first item is accessed using the index of the array
var datereturn = JSON.parse(result.d)[0];
 
Share this answer
 
Here is an example. I'm assuming the JSON string is as you describe in the post. Add the quote around the datetime value.
"[{\"id\":1,\"datetime\":04/10/2017,\"col1\":1,\"col2\":2,\"col3\":2}]"

read json array - JSFiddle[^]

Here are the output from the sample code:
ID: 1
datetime: 04/10/2017
col3: 2

ID: 2
datetime: 05/10/2017
col3: 33

The result will be blank if you remove the quote from the datetime value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900