我获得了一个示例页面以从中获取字符串。我将此sting转换为可用于不同代码的变量。我一直试着从网页上获取数据,但我很遗憾。
首先,我创建了一个示例代码来阅读页面并使用Chrome控制台,我可以看到我需要的单词。这是示例代码
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#loaddata").click(function(){
$.post("http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",function(data){
console.log(data);
datafromwebsite = data;
magicWordResult = "Your word to guess is " + datafromwebsite;
alert(magicWordResult);
});
});
});
</script>
</head>
<body>
<div>Get the object from sample page:</div>
<button id="loaddata">Load data</button>
</body>
</html>
现在我创建了第二个代码来获取字符串并创建我的变量,但我不知道如何做到这一点。我研究过书籍和网站,我不确定。有什么建议?感谢你们。这是我正在使用的代码:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#loaddata").click(function(){
$.ajax({
type: "POST",
url: "http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",
dataType: "json",
success:function(data){
console.log(data);
}
});
});
});
</script>
</head>
<body>
<div>Get the object from sample page:</div>
<button id="loaddata">Load data</button>
</body>
</html>
答案 0 :(得分:0)
第一个jQuery脚本和第二个jQuery脚本之间的唯一区别是第二个脚本指定返回的数据是JSON格式。
你实际上可以在&#34;简写&#34;中编写第二个脚本。形式是这样的:
$(document).ready(function(){
$("#loaddata").click(function() {
$.getJSON("http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord", function(data) {
console.dir(data);
});
});
});
与第一个非常相似。
答案 1 :(得分:0)
以下是console.dir(log)
URL: ""
activeElement: null
anchors: HTMLCollection[0]
applets: HTMLCollection[0]
baseURI: null
body: null
characterSet: null
charset: undefined
childElementCount: 1
childNodes: NodeList[1]
children: HTMLCollection[1]
compatMode: "CSS1Compat"
contentType: "application/xml"
cookie: [Exception: DOMException]
currentScript: nulldefaultCharset: undefined
defaultView: null
designMode: "off"
dir: ""
doctype: null
documentElement: string
documentURI: null
domain: ""
embeds: HTMLCollection[0]
firstChild: string
firstElementChild: string
fonts: FontFaceSet
forms: HTMLCollection[0]
head: nullhidden: true
images: HTMLCollection[0]
implementation: DOMImplementation
inputEncoding: nulllastChild: string
lastElementChild: string
lastModified: "09/07/2014 21:54:24"
links: HTMLCollection[0]
localName: null
location: null
namespaceURI: null
nextSibling: null
nodeName: "#document"
nodeType: 9
nodeValue: null
onabort: null
onautocomplete: null
onautocompleteerror: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onblur: null
oncancel: null
oncanplay: null
oncanplaythrough: null
onchange: null
onclick: null
onclose: null
oncontextmenu: null
oncopy: null
oncuechange: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
ondurationchange: null
onemptied: null
onended: null
onerror: null
onfocus: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onloadeddata: null
onloadedmetadata: null
onloadstart: null
onmousedown: null
onmouseenter: null
onmouseleave: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmousewheel: null
onpaste: null
onpause: null
onplay: null
onplaying: null
onpointerlockchange: null
onpointerlockerror: null
onprogress: null
onratechange: null
onreadystatechange: null
onreset: null
onresize: null
onscroll: null
onsearch: null
onseeked: null
onseeking: null
onselect: null
onselectionchange: null
onselectstart: null
onshow: null
onstalled: null
onsubmit: null
onsuspend: null
ontimeupdate: null
ontoggle: null
onvolumechange: null
onwaiting: null
onwebkitfullscreenchange: null
onwebkitfullscreenerror: null
onwebkitpointerlockchange: null
onwebkitpointerlockerror: null
onwheel: null
ownerDocument: null
parentElement: null
parentNode: null
plugins: HTMLCollection[0]
pointerLockElement: null
preferredStylesheetSet: null
previousSibling: null
readyState: "interactive"
referrer: ""
rootElement: null
scripts: HTMLCollection[0]
selectedStylesheetSet: null
styleSheets: StyleSheetList
textContent: null
title: ""
visibilityState: "hidden"
webkitCurrentFullScreenElement: null
webkitFullScreenKeyboardInputAllowed: false
webkitFullscreenElement: null
webkitFullscreenEnabled: true
webkitHidden: true
webkitIsFullScreen: false
webkitPointerLockElement: null
webkitVisibilityState: "hidden"
xmlEncoding: "utf-8"
xmlStandalone: false
xmlVersion: "1.0"__proto__: XMLDocument
答案 2 :(得分:0)
而不是
$.ajax({
type: "POST",
url: "http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",
dataType: "json",
success:function(data){
console.log(data);
}
});
试
$.get('http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord', function(xml) {
// this should contain your magic word
console.log(xml.childNodes[0].innerHTML);
}, 'xml');