我的剧本是让从URL JSON文件中的文本框从文本框中获取JSON网址?
我IDed文本框为txtr
和使用jQuery中获取价值var txtbval = $("#txtr").val();
进入,这在JSON解析脚本url: txtbval,
但是,当我点击按钮,没有发生我不知道我做错了什么,我不知道,这种方法可以在JSON解析
的index.php
<!doctype html>
<html>
<head>
<title>How to Parse a JSON file using jQuery</title>
<style>
body{
text-align: center;
font-family: arial;
}
.button{
margin:20px;
font-size:16px;
font-weight: bold;
padding:5px 10px;
}
</style>
</head>
<body>
<textarea id=txtr" rows="4" cols="50">
</textarea>
<input type="button" value="Get and parse JSON" class="button" />
<br />
<span id="results"></span>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//When DOM loaded we attach click event to button
$(document).ready(function() {
var txtbval = $("#txtr").val();
//after button is clicked we download the data
$('.button').click(function(){
//start ajax request
$.ajax({
url: txtbval,
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
var json = $.parseJSON(data);
//now json variable contains data in json format
//let's display a few items
$('#results').html(json.post.title);
}
});
});
});
</script>
</body>
</html>
使用
评论数据类型选项,直接检查,data.post.title – rahulsm
当你知道你会得到响应JSON那么你为什么这样做是JSON解析?只需设置'dataType:'json'' – itzmukeshy7