Link to home
Start Free TrialLog in
Avatar of Arnold Layne
Arnold LayneFlag for United States of America

asked on

JSON results only display as a boolean value of true.

I am running some very basic tests and I am using jQuery to make an ajax get call from page A on my domain to page B on my domain. Page B server side code parses the request and builds a google maps url based on what was passed in by the "find" parameter of the query string. Then page B uses that to make a post request to the built google maps url. Then it ouputs the json it receives back on to page B and I can see the json data, which means that my jQuery get from page A should receive whatever page B outputs to it's page and I would expect to see this data on page A.

The problem is that page A seems to receive back a boolean value of true, and that's what it outputs/displays on the page . However, when I look at the page A response results in the chrome dev console under the network tab and then under response, I see all of the json data that I expected to see on the page. There is some little detail that I don't understand about json. Any help would be greatly appreciated. Hopefully an easy question.

Page A
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tsp.aspx.cs" Inherits="tsp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id ="tsp">
    </div>
    </form>
    <script type="text/javascript">
        var addresses = new Array();
        var test = encodeURIComponent('');
        var find = encodeURIComponent("origin=343 Billerica Avenue Billerica MA&destination=92 Swan Street Lowell ma&waypoints=optimize:true|210 Chelsmford Street Chelmsford MA|335 Chelmsford Street Chelmsford MA&key=AIzaSyAXZN22Ix19d9H9j5uKV-RKdntdg5FH_j4");
        console.trace(find);
        $.get("proxy.aspx?find=" + find, function (data) {
                if (data = ! null || data != undefined) {
                    $("#tsp").text(data);
                }
                else { alert("I got nothin"); }
            }
        );
    </script>
</body>
</html>

Open in new window



Page B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

public partial class proxy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string find = Request["find"];
        string myResponse = "";
        string myUrl = "https://maps.googleapis.com/maps/api/directions/json?" + find;
        System.IO.StreamWriter myWriter = null;// it will open a http connection with provided url
        System.Net.HttpWebRequest objRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(myUrl);//send data using objxmlhttp object
        objRequest.Method = "POST";
        //objRequest.ContentLength = TranRequest.Length;
        objRequest.ContentType = "application/json";//to set content type
        myWriter = new System.IO.StreamWriter(objRequest.GetRequestStream());
        myWriter.Write(find);//send data
        myWriter.Close();//closed the myWriter object

        System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();//receive the responce from objxmlhttp object 
        using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
        {
            myResponse = sr.ReadToEnd();
        }
        Response.Write("<div>" + myResponse + "</div>");
    }
}

Open in new window

Avatar of Hans Langer
Hans Langer

1) B is returning the result into tags:
Response.Write("<div>" + myResponse + "</div>");

Open in new window

so it will not be a valid json

2) try jQuery.getJSON()  or $.ajax({ dataType: "json"......}); to specify that the result will be a json, so you get the result parsed as a json object. http://api.jquery.com/jquery.getjson/

3) you should add a validation in "B" to return an empty json to avoid errors in "A"
Avatar of Arnold Layne

ASKER

Hi Gerente. I used to not have the surrounding divs. I just tried that to see if it works, but it didn't. So it doesn't work with them and doesn't work without them. It just always returns "true". But, in my chrome dev console, it shows that the json did get returned and shows it all to me. So there is something wrong with me using "data" the way I am, and I need to use something like responsetext, because the data variable = true.
ASKER CERTIFIED SOLUTION
Avatar of Hans Langer
Hans Langer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem is that I was trying to access the "data" variable that I put the results into, when I needed to access data.routes, which is the first item in the json results. So its actually correct for the data variable to be a value of true, and it's elements are the things that have useable values.
im glad you could fix your problem. Next try put that information too so we can help you better.