将 json 对象从 php 分配给 javascript 有什么问题吗?
我需要将 php json 对象的值传递给 javascript。我就是这样做的;
<script type="text/javascript"> var msg_top = <?php echo searchResults('windows');?>;
</script>
这是返回 json 的 php SearchResults 函数:
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return json_encode($xml);
}
错误控制台中没有错误,但没有收到任何值。它输出类似 array(0) { } for var_dump($_POST); 编辑 我在成功功能后添加了一个测试警报,它打印出失败
这是脚本
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
</script>
</head>
<body>
<script type="text/javascript"> var msg_top = <?php echo searchResults('windows');?>;
</script>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
url: "script.php",
type: "POST",
dataType: "json",
data: "msg_top",
success: function(msg){
alert("success");
}
});
alert("failure");
});
</script>
</body>
</html>
,这是来自 Twitter 的 json
<script type="text/javascript"> var test_json = {\"id\":\"tag:search.twitter.com,2005:search\\/#DIYSe_F\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/search.twitter.com\\/search?q=%23DIYSe_F\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100\",\"rel\":\"self\"}},{\"@attributes\":{\"type\":\"application\\/opensearchdescription+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/opensearch.xml\",\"rel\":\"search\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100&since_id=7750301532557312\",\"rel\":\"refresh\"}}],\"title\":\"#DIYSe_F - Twitter Search\",\"updated\":\"2010-11-24T22:20:44Z\",\"entry\":[{\"id\":\"tag:search.twitter.com,2005:7559269595488256\",\"published\":\"2010-11-24T22:20:44Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7559269595488256\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":
\":\"image\"}}],\"title\":\"#DIYse_F HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities types\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities types\",\"updated\":\"2010-11-24T22:20:44Z\",\"author\":{\"name\":\"_smir (Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7552659368189952\",\"published\":\"2010-11-24T21:54:28Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7552659368189952\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra
\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"updated\":\"2010-11-24T21:54:28Z\",\"author\":{\"name\":\"_smir (Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7548895705956352\",\"published\":\"2010-11-24T21:39:31Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\\/statuses\\/7548895705956352\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/a1.twimg.com\\/profile_images\\/1090185625\\/29465_391454998679_533808679_3864564_6071800_n_normal.jpg\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"updated\":\"2010-11-24T21:39:31Z\",\"author\":{\"name\":\"Babar_Shahzad (Babar Shahzad Ch)\",\"uri\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\"}}]};
I need to pass the value of a php json object to javascript. This is how I am doing it;
<script type="text/javascript"> var msg_top = <?php echo searchResults('windows');?>;
</script>
This is php SearchResults function that returns json:
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return json_encode($xml);
}
There is no error in error console but there is no value received. It outputs like array(0) { } for var_dump($_POST);
EDIT
I added a test alert after success function and it prints out failure
This is the the script
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
</script>
</head>
<body>
<script type="text/javascript"> var msg_top = <?php echo searchResults('windows');?>;
</script>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
url: "script.php",
type: "POST",
dataType: "json",
data: "msg_top",
success: function(msg){
alert("success");
}
});
alert("failure");
});
</script>
</body>
</html>
this is json from twitter
<script type="text/javascript"> var test_json = {\"id\":\"tag:search.twitter.com,2005:search\\/#DIYSe_F\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/search.twitter.com\\/search?q=%23DIYSe_F\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100\",\"rel\":\"self\"}},{\"@attributes\":{\"type\":\"application\\/opensearchdescription+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/opensearch.xml\",\"rel\":\"search\"}},{\"@attributes\":{\"type\":\"application\\/atom+xml\",\"href\":\"http:\\/\\/search.twitter.com\\/search.atom?q=%23DIYSe_F&rpp=100&since_id=7750301532557312\",\"rel\":\"refresh\"}}],\"title\":\"#DIYSe_F - Twitter Search\",\"updated\":\"2010-11-24T22:20:44Z\",\"entry\":[{\"id\":\"tag:search.twitter.com,2005:7559269595488256\",\"published\":\"2010-11-24T22:20:44Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7559269595488256\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":
\":\"image\"}}],\"title\":\"#DIYse_F HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities types\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> HIE_STRUCT: HIERARCHICAL STRUCTURE: hierarchical structure to display \\nmessages of Functions and Qualities types\",\"updated\":\"2010-11-24T22:20:44Z\",\"author\":{\"name\":\"_smir (Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7552659368189952\",\"published\":\"2010-11-24T21:54:28Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/_smir\\/statuses\\/7552659368189952\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/s.twimg.com\\/a\\/1289849896\\/images\\/default_profile_5_normal.png\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra
\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> SEND_MSG: users can send messages of four types i.e. \\n\\nFunction,Quality,Solution, and delivery\",\"updated\":\"2010-11-24T21:54:28Z\",\"author\":{\"name\":\"_smir (Smeer)\",\"uri\":\"http:\\/\\/twitter.com\\/_smir\"}},{\"id\":\"tag:search.twitter.com,2005:7548895705956352\",\"published\":\"2010-11-24T21:39:31Z\",\"link\":[{\"@attributes\":{\"type\":\"text\\/html\",\"href\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\\/statuses\\/7548895705956352\",\"rel\":\"alternate\"}},{\"@attributes\":{\"type\":\"image\\/png\",\"href\":\"http:\\/\\/a1.twimg.com\\/profile_images\\/1090185625\\/29465_391454998679_533808679_3864564_6071800_n_normal.jpg\",\"rel\":\"image\"}}],\"title\":\"#DIYse_F READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"content\":\"<a href=\\\"http:\\/\\/search.twitter.com\\/search?q=%23DIYse_F\\\" onclick=\\\"pageTracker._setCustomVar(2, \'result_type\', \'recent\', 3);pageTracker._trackPageview(\'\\/intra\\/hashtag\\/#DIYse_F\');\\\"><b>#DIYse_F<\\/b><\\/a> READ_MSG: Users can read messages of all four types in \\n\\ndifferent windows\",\"updated\":\"2010-11-24T21:39:31Z\",\"author\":{\"name\":\"Babar_Shahzad (Babar Shahzad Ch)\",\"uri\":\"http:\\/\\/twitter.com\\/Babar_Shahzad\"}}]};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
json_encode
将数据编码为 JSON,则输出已经是有效的JavaScript 表达式。所以你不需要把它放在引号中。事实上,对其应用
addslashes
会将有效的 JavaScript 表达式转换为包含该表达式的字符串JavaScript 表达式:在 JavaScript 中使用这些值时,
$json
将包含有效的对象表达式,而$str
将包含字符串表达式:这将变为:
所以只需使用
json_encode
而不应用任何进一步的编码/转义。在您的情况下(因为searchResults
确实已经返回 JSON 字符串):If you use
json_encode
to encode the data as JSON, the output will already be a valid JavaScript expression. So you don’t need to put it into quotes.In fact, applying
addslashes
on it will turn the valid JavaScript expression into a string containing that JavaScript expression:When using these values in JavaScript,
$json
will contain a valid object expression while$str
will contain a string expression:This will become:
So just use
json_encode
without applying any further encoding/escaping. In your case (assearchResults
does already return a JSON string):如果 searchResults 函数尚未返回 JSON,那么 json_encode() 会更容易,例如,
如果 searchResults函数已经返回正确的 JSON,您应该能够直接分配输出,例如
If the searchResults function doesn't already return JSON, then json_encode() would be easier, e.g.
If the searchResults function already returns proper JSON, you should just be able to assign the output directly, e.g.