jq beginner
jq beginner

Reputation: 1070

provlem with ajax returning the whole html code

i'm trying to get some data by ajax

var ajax1 = false;
ajax1 = new XMLHttpRequest();
ajax1.open("GET","ajax/getolinedata.php");
ajax1.onreadystatechange = function(){
    var mylink = ajax1.responseText;
    $("a[goal='online']").attr("href",mylink);
}
ajax1.send(null);

and this is the php code returning the data

    while($cartRow = mysql_fetch_array($getCartR)){
    $pro_name = $cartRow['Product_Name'];
    if(strstr($pro_name," ")){
        $pro_name = str_replace(" ","_",$cartRow['Product_Name']);
    }
    $lin .= "&li_".$x."_type=product&li_".$x."_price=".$cartRow['Product_Price']."&li_".$x."_quantity=".$cartRow['Quantity']."&li_".$x."_name=".$pro_name."&li_".$x."_tangible=N";
    $x++;
}
echo $lin;

but the result is this

<a href="&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd&quot;&gt;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;https://www.2checkout.com/checkout/purchase?sid=123456&amp;mode=2CO&amp;li_0_type=product&amp;li_0_price=3700&amp;li_0_quantity=1&amp;li_0_name=iphone_4&amp;li_0_tangible=N&amp;li_1_type=product&amp;li_1_price=3000&amp;li_1_quantity=1&amp;li_1_name=Lumia_720&amp;li_1_tangible=N&amp;li_2_type=product&amp;li_2_price=4500&amp;li_2_quantity=1&amp;li_2_name=ipad_2&amp;li_2_tangible=N&amp;li_3_type=product&amp;li_3_price=2000&amp;li_3_quantity=2&amp;li_3_name=Lumia_520&amp;li_3_tangible=N" goal="online"><img src="images/online.jpg"></a>

it's returning the whole html while there's no html code in that php page at all it's just php codes to connect to the database and fetch data any help ?

Upvotes: 1

Views: 1868

Answers (2)

Arslan Bilal
Arslan Bilal

Reputation: 625

Make Sure You didn't print the html in config file or constructor or something like that. I got the the same problem. Solution is to create separate file to handle the ajax request.

Upvotes: 0

Pruthvi Raj Nadimpalli
Pruthvi Raj Nadimpalli

Reputation: 1373

In ajax/getolinedata.php if you have html code or echo html code, it will return the HTML code too.

Try opening ajax/getolinedata.php directly on your browser. If you get HTML page, then the same will be sent to ajax request.

So, try echoing only information that is needed and you should be good.

Also don't forget to set response type in header. For example if you are returning json data, use:

header('Content-type: text/json');

Upvotes: 3

Related Questions