2012-03-16 81 views
0

您好我正在尝试使用jquery运行ajax查询。如果我使用本地存储的.txt文件,我不会遇到任何问题。每当我尝试查询一个php生成的json时,问题就来了。这是代码:可以从.txt文件获取json,但不能从php生成json使用jquery

$('#find').click(function(){ 
$.getJSON('http://localhost/public/ProductCatalog/searchindex/txt.php', function(data) { 
var items = []; 
$.each(data, function(key, val) { 
     pushStr = '<div class="prod-container">'; 
     pushStr += ' <div class="prod-image-container"><img class="prod-img" src="' + val['foto'] + '"/></div>'; 
     pushStr += ' <div class="prod-desc-container">' + val['title'] + '</div>'; 
     pushStr += ' <input class="id" type="hidden" value="' + val['id'] + '"/>'; 
     pushStr += ' <input class="title" type="hidden" value="' + val['title'] + '"/>'; 
     pushStr += '</div>'; 
     items.push(pushStr); 
    }); 

    items.push('<div style="clear:both;"></div>'); 
    $('#prod-body').html(items.join('')); 
    $('img.prod-img').each(function (index, element){ 
     fitImage(element, 75, 110); 
    }); 
    makeDraggable(); 
}); 
}); 

所以,如果我创建file.txt的复制/粘贴相同的信息,我从http://localhost/public/ProductCatalog/searchindex/txt.php生成和使用它作为一个参数,然后我得到的内容。当我按原样运行代码时,什么都得不到执行。

file.txt的例子:

{ 
    "item1": { 
     "foto": "item1.jpg", 
     "title": "Teclado roland fantom-g8 las teclas con contrapeso", 
     "id": "1", 
     "price": "56090.25" 
    }, 
    "item2": { 
     "foto": "item2.jpg", 
     "title": "Teclado roland v-piano lo cambia todo", 
     "id": "1", 
     "price": "85501.79" 
    }, 
    "item3": { 
     "foto": "item3.jpg", 
     "title": "Teclado roland ax-synth teclado 49 teclas (dinÃ", 
     "id": "1", 
     "price": "13034.05" 
    }, 
    "item4": { 
     "foto": "item4.jpg", 
     "title": "Teclado roland fantom g-6 fuente de sonido avanzada", 
     "id": "1", 
     "price": "39989.14" 
    }, 
    "item5": { 
     "foto": "item5.jpg", 
     "title": "Teclado gw-8l roland gw-8l -bstock", 
     "id": "1", 
     "price": "11627.32" 
    }, 
    "item6": { 
     "foto": "item6.jpg", 
     "title": "Teclado disney", 
     "id": "1", 
     "price": "605.00" 
    } 
} 

这里是PHP代码:

// action body usinf zend framework 
    $this->getHelper('viewRenderer')->setNoRender(); 
    $index = Zend_Search_Lucene::open('/data/prod-catalog'); 
    $results = $index->find('teclado roland'); 
    $first = $this->_request->getParam('first'); 

    header('Cache-Control: no-cache, must-revalidate'); 
    header('Content-type: application/json'); 

    $i=1; 

    echo '{'; 
    foreach ($results as $result){ 
     echo '"item'.$i.'": {'."\n"; 
     echo ' "foto": "'.$result->foto.'",'."\n"; 
     echo ' "title": "'.ucfirst(strtolower($result->titulo)).'",'."\n"; 
     echo ' "id": "'.'1'.'",'."\n"; 
     echo ' "price": "'.ucfirst(strtolower($result->precio)).'"'."\n"; 
     echo ($i<count($results) && $i<6)? '},'."\n": '}'."\n".'}'; 
     $i++; 
     if($i==7){ 
      break; 
     } 
    } 
+0

你的数据包嗅探器说你得到了什么? – 2012-03-16 03:04:25

+0

对不起,我完全不理解你,但浏览器控制台实际上下载文件,它只是不执行任何其他事情。 – awavi 2012-03-16 03:11:45

+0

你能发布txt.php的内容吗?只需要确定 – 2012-03-16 03:12:22

回答

1

我发现它,这是一个安全问题,我在本地运行带有js功能的html,并且服务器不允许这样做,一旦我将它运行在服务器上,它就解决了。

0

尝试逃避“或”从你的字符串时您正在使用和addslashes()建立JSON

+0

我已经这样做了,仍然没有回应 – awavi 2012-03-16 17:06:05

+0

好的,我们需要你的txt.php输出。 – 2012-03-17 03:09:32