1

I am making an ajax request to a php page and I cannot for the life of me get it to just return plain text.

    $.ajax({
       type: 'GET',
       url: '/shipping/test.php',
       cache: false,
       dataType: 'text',
       data: myData,   
       success: function(data){
        console.log(data)
       }   
     });

in test.php I am including this script to get UPS rates. I am then calling the function with $rate = ups($dest_zip,$service,$weight,$length,$width,$height);

I am doing echo $rate; at the bottom of test.php. When viewed in a browser shows the rate, that's great. But when I request the page via ajax I get a bunch of XML. Pastie here: http://pastie.org/1416142

My question is, how do I get it so I can just return the plain text string from the ajax call, where the result data will be a number?

Edit, here's what I see in Firebug- Response tab:

alt text

HTML tab:

alt text

jyoseph
  • 5,435
  • 9
  • 45
  • 64
  • Have you also had a look at the *source* of the page? Is it still plain text? If your PHP script returns XML then you will get XML. – Felix Kling Dec 30 '10 at 00:29
  • Yeah, it's the XML found in the pastie with my string at the end. Here's another pastie, you have to scroll way way to the right to see it though: http://pastie.org/1416155 – jyoseph Dec 30 '10 at 00:30
  • Which number do you want? You could either extract the number with PHP beforehand or with jQuery. – Felix Kling Dec 30 '10 at 00:32
  • That's what is weird. If I do $rate = ups($dest_zip,$service,$weight,$length,$width,$height); then the $rate variable has the number I want. I would like to just return that. But I think by nature of calling the ups() function, it's putting xml on the page too. – jyoseph Dec 30 '10 at 00:38

4 Answers4

2

The PHP script you are using outputs XML. Look at upsRate.php and you will see a whole bunch of XML in a string.

Also you might want to comment out line 96 of upsRate.php because it outputs some comments which you will see when you fetch the page with ajax.

Samuel
  • 16,923
  • 6
  • 62
  • 75
2

Don't suppress the XML, use it. That's what AJAX was always intended for. You will want to simply parse the XML and get the data you need from it, which fortunately, JQuery makes fairly easy.

First, switch your AJAX data type:

dataType: 'xml'

Then, in your ajax response handler:

alert($(xml_data).find("PriceXMLNodeName").text());

You will of course have to figure out and replace PriceXMLNodeName with the actual name of the node the price is in within the XML response. I cannot see what the node name is from your screenshot. Should be pretty simple to see it though (It's whatever the name of the tag the price is incased in).

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
1

It looks like the ups() function returns XML, not plain text. You have two possible options:

  1. Check if the API you are using provides a function that returns plain text instead of XML. This would probably be the ideal solution.
  2. Parse the XML yourself, and echo the value you are interested in. The following should do the trick:
    $rate = ups($dest_zip,$service,$weight,$length,$width,$height);
    $x = simplexml_load_string($rate);
    echo $x->RatedShipment->TransportationCharges->MonetaryValue;

EDIT: just noticed that the ups() function echo's the HTTP header and response XML within an HTML comment, so it really doesn't do anything other than mess you up. If possible, remove that echo statement from ups() and you should be golden.

mfonda
  • 7,873
  • 1
  • 26
  • 30
1

I'm going to put this as an answer because it solved the immediate solution, suppressing the xml output. Taken from this SO question.

ob_start();
$rate = ups($dest_zip,$service,$weight,$length,$width,$height);
ob_end_clean();

That allowed me to use the $rate var (which contains the value I want) w/out getting the xml. I don't know if it's right or not so I'll let others address it.

Community
  • 1
  • 1
jyoseph
  • 5,435
  • 9
  • 45
  • 64
  • It's just solving it a different way then the rest of the answers. The good thing about it is that you didn't have to alter the library at all. Nice work! – Samuel Dec 30 '10 at 00:49