dtamho6404 2014-10-28 20:59
浏览 39

将JSON从PHP传递给ajax()

This is my ajax():

<script>
        $(document).ready(function(){
            $("form").submit(function(){
                var url = "xxx.php/";
                var param = $("#streetInput").serialize() + "&";
                    param += $("#cityInput").serialize() + "&";
                    param += $("#stateInput").serialize();

                    $.ajax({
                            url: url,
                            data: param,
                            type: 'GET',
                            dataType: 'json',
                            success: function(data) {
                                // parse the data here
                                alert(data);
                            },
                            error: function(){
                                alert("error");
                            }
                        });
            });

        });
</script>

And I successfully pass parameters to xxx.php file, and also convert some data to JSON form and echo them correctly.

<?php
$streetArr = array(); $cityArr = array();
$street = $city = $state = "";
$url = 'http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=myid&';


if($_SERVER["REQUEST_METHOD"] == "GET")
{

    $street = test_input($_GET["streetInput"]);
    $city = test_input($_GET["cityInput"]);
    $state = test_input($_GET["stateInput"]);
    $streetArr = strsToArray($street);
    $cityArr = strsToArray($city);

    $url = $url . "address=" . $streetArr[0];
    for($x = 1; $x < count($streetArr); $x++)
        if($streetArr[$x] != "#")
            $url .= "+$streetArr[$x]";

    $url = $url . "&citystatezip=" . $cityArr[0];
    for($x = 1; $x < count($cityArr); $x++)
        $url .= "+$cityArr[$x]";

    $url = $url . "%2C+" . $state . "&rentzestimate=true";

    $xml = simplexml_load_file($url);
    echo json_encode($xml);

    ..... // some functions
}

but when I debug on Firefox with Firebug, I cannot get "alert(data)" but "alert("error")" instead. I don't know why my ajax() request not success.

I get the JSON data in server-side:

{"request":{"address":"2636 Menlo Street","citystatezip":"Los Angeles, CA"},"message":{"text":"Request successfully processed","code":"0"},"response":{"results":{"result":{"zpid":"20593083","links":{"homedetails":"http:\/\/www.zillow.com\/homedetails\/2636-Menlo-Ave-Los-Angeles-CA-90007\/20593083_zpid\/","graphsanddata":"http:\/\/www.zillow.com\/homedetails\/2636-Menlo-Ave-Los-Angeles-CA-90007\/20593083_zpid\/#charts-and-data","mapthishome":"http:\/\/www.zillow.com\/homes\/20593083_zpid\/","comparables":"http:\/\/www.zillow.com\/homes\/comps\/20593083_zpid\/"},"address":{"street":"2636 Menlo Ave","zipcode":"90007","city":"Los Angeles","state":"CA","latitude":"34.03163","longitude":"-118.289972"},"FIPScounty":"6037","useCode":"Duplex","taxAssessmentYear":"2013","taxAssessment":"152082.0","yearBuilt":"1924","lotSizeSqFt":"5242","finishedSqFt":"1728","bathrooms":"2.0","bedrooms":"2","lastSoldDate":"07\/29\/1996","lastSoldPrice":"115000","zestimate":{"amount":"452915","last-updated":"10\/26\/2014","oneWeekChange":{"@attributes":{"deprecated":"true"}},"valueChange":"3507","valuationRange":{"low":"375919","high":"520852"},"percentile":"0"},"rentzestimate":{"amount":"1887","last-updated":"10\/27\/2014","oneWeekChange":{"@attributes":{"deprecated":"true"}},"valueChange":"-9","valuationRange":{"low":"1415","high":"2680"}},"localRealEstate":{"region":{"@attributes":{"id":"268581","type":"neighborhood","name":"West Adams"},"links":{"overview":"http:\/\/www.zillow.com\/local-info\/CA-Los-Angeles\/West-Adams\/r_268581\/","forSaleByOwner":"http:\/\/www.zillow.com\/west-adams-los-angeles-ca\/fsbo\/","forSale":"http:\/\/www.zillow.com\/west-adams-los-angeles-ca\/"}}}}}}}

After I click submit button, then it shows alert("error") first and then redirect to xxx.php as I specified in

  • 写回答

2条回答 默认 最新

  • dongrong8972 2014-10-28 22:36
    关注

    You should set the content type in the header at the PHP code and you mustn't print anything except the json_encoded data

    header('Content-type: application/json');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?