开发者

jQuery ajax request to php, how to return plain text only

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 o开发者_运维知识库f 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:

jQuery ajax request to php, how to return plain text only

HTML tab:

jQuery ajax request to php, how to return plain text only


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.


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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜