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">
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <form id="form1" runat="server">
    <div id ="tsp">
    <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");
        $.get("proxy.aspx?find=" + find, function (data) {
                if (data = ! null || data != undefined) {
                else { alert("I got nothin"); }

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hans LangerCommented:
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"
BobHavertyComhAuthor Commented:
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.
Hans LangerCommented:
if you know that the page B is returning properly the json string (try it putting directly the url in the browser "proxy.aspx?find=XXXX") then it could be an access error. Are both pages in the same domain AND subdomain? otherwise it can be related with the crossdomain access, then you would need to add a custom header to your response. You can add in your webconfig the following tags to add a custom response.

                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="origin, content-type, accept" />
                <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />                                

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
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.
Hans LangerCommented:
im glad you could fix your problem. Next try put that information too so we can help you better.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.