In shared folder of ASP.NET C#, I create a .cshtml which defines a button that can GET data from an API. I would like to generate an url and use it to update an iframe of a viewer.
JavaScript
x
30
30
1
function callAPI(searchText) {
2
$.ajax({
3
url: '/Home/CallAPI',
4
data: { Text: searchText },
5
type: "GET",
6
success: function (result) {
7
var data = JSON.stringify(result); // server response
8
found_data = data;
9
$(result).each(function () {
10
if (this.status == "found") {
11
alert("Found!" + data);
12
var frameElement = document.getElementById("SearchingMap");
13
lon = result.results[0].lon;
14
lat = result.results[0].lat;
15
new_searching_url = "http://xx.xxx.xxx.xxx:8080/styles/osm-bright-tw/#17/" + lat.toString() + "/" + lon.toString();
16
console.log(frameElement.src); // undefined
17
frameElement.src = new_searching_url;
18
console.log(frameElement.src); // "http://xx.xxx.xxx.xxx:8080/styles/osm-bright-tw/#17/.../..."
19
}
20
else {
21
alert("Sorry! Not found");
22
}
23
});
24
},
25
error: function () {
26
alert("Sorry! Not found");
27
}
28
});
29
}
30
However, the iframe in the viewer, which named SearchingMap.cshtml, doesn’t updated.
JavaScript
1
6
1
@{ViewBag.Title = "SearchingMap";}
2
<div id="SearchingMap">
3
<h3>Searching map</h3>
4
<iframe src="http://xx.xxx.xxx.xxx:8080/styles/osm-bright-tw/#10.01/25.0709/121.5008" frameborder="0" scrolling="no">Your browser doesn't support iframe.</iframe>
5
</div>
6
Why can’t it work? How can I update the iframe of a viewer?
Advertisement
Answer
Here the iframe
did not have the id SearchingMap
so all javascript code fails because of this line:
JavaScript
1
2
1
var frameElement = document.getElementById("SearchingMap");
2
Just add this id, on your iframe
JavaScript
1
2
1
<iframe id="SearchingMap"
2