{"id":1372,"date":"2022-08-30T15:15:59","date_gmt":"2022-08-30T15:15:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/15\/changing-src-on-img-tag-cant-make-it-work-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:59","modified_gmt":"2022-08-30T15:15:59","slug":"changing-src-on-img-tag-cant-make-it-work-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/changing-src-on-img-tag-cant-make-it-work-collection-of-common-programming-errors\/","title":{"rendered":"Changing src on img tag, can&#39;t make it work-Collection of common programming errors"},"content":{"rendered":"<p>There are several problems I found and wrestled with, but finally got some working code:<\/p>\n<pre><code>\n\n\n\n\n\n<img loading=\"lazy\" decoding=\"async\" id=\"a1\" src=\"http:\/\/www.containernurseries.co.nz\/images\/contacticon.gif\" width=\"18\" height=\"37\" \/>\n\n\n\n\n    $.getJSON(\"http:\/\/ip.jsontest.com\", null,\n            function (data) {\n                console.log(data)\n            });\n    $(document).ready(function () {\n        ($('#a1')).mouseover(function (e) {\n            ($('#a1').attr('src', 'http:\/\/www.containernurseries.co.nz\/images\/services.gif'));\n        }).mouseout(function (e) {\n                    ($('#a1')).attr('src', 'http:\/\/www.containernurseries.co.nz\/images\/contacticon.gif');\n                });\n    });\n\n\n\n<\/code><\/pre>\n<p>You can see it work here:<\/p>\n<p>http:\/\/www.sanbarcomputing.com\/flat\/forumPosts\/imgSrcType\/imgSrcTypeNoComments.html<\/p>\n<p>This code uses a service called &#8220;JSONTest&#8221; to get properly formatted JSON code. This returns an object (data) of key\/value {ip: &#8220;xxx.xxx.xxx.xxx&#8221;} which shows your ip address. Here is the services website that I use to get the JSON response:<\/p>\n<p>http:\/\/teamaqua.github.com\/JSONTest\/<\/p>\n<p>To see the console log output, just open a console in your browser (hit the F12 key, for instance, or open the FireBug plugin for FireFox. Drill down into the object to see the key\/value pair properly formatted in the console.<\/p>\n<p>I fixed your code with the scr-&gt;src typo fix and some other things needing fixing:<\/p>\n<pre><code>\n\n\n\n\n\n<img loading=\"lazy\" decoding=\"async\" id=\"a1\" src=\"http:\/\/www.containernurseries.co.nz\/images\/contacticon.gif\" width=\"18\" height=\"37\" \/>\n\n\n\n\n    $.getJSON(\"http:\/\/www.containernurseries.co.nz\/json\/jsonPlantDetails.php\",\n            {plantSelected:\"ARGYRANTHEMUM-POLLY\"},\n            function (data) {\n                ($('#a1')).attr('src', 'data:image\/jpg;base64,' + data.plantDetail.Image);\n            });\n    $(document).ready(function () {\n        ($('#a1')).mouseover(function (e) {\n            ($('#a1').attr('src', 'http:\/\/www.containernurseries.co.nz\/images\/services.gif'));\n        }).mouseout(function (e) {\n                    ($('#a1')).attr('src', 'http:\/\/www.containernurseries.co.nz\/images\/contacticon.gif');\n                });\n    });\n\n\n\n<\/code><\/pre>\n<p>You can see it (possibly) fail here:<\/p>\n<p>http:\/\/www.sanbarcomputing.com\/flat\/forumPosts\/imgSrcType\/imgSrcType.html<\/p>\n<p>I get a cross-domain error in Chrome, and it seems to fail silently in IE and FireFox:<\/p>\n<blockquote>\n<p>XMLHttpRequest cannot load http:\/\/www.containernurseries.co.nz\/json\/jsonPlantDetails.php?plantSelected=ARGYRANTHEMUM-POLLY. Origin http:\/\/www.sanbarcomputing.com is not allowed by Access-Control-Allow-Origin.<\/p>\n<\/blockquote>\n<p>Here is a good post that talks about one way to fix this (changing it to JSONP), but since your server returns JSON, not JSONP, it does not work either (I tried):<\/p>\n<p>stackoverflow: access-control-allow-origin-not-allowed-by<\/p>\n<p>You would need to return the result in the form of a JSONP JavaScript executable function from the server, I believe. To get a JSONP request sent, you would change this line:<\/p>\n<pre><code>$.getJSON(\"http:\/\/www.containernurseries.co.nz\/json\/jsonPlantDetails.php\",\n<\/code><\/pre>\n<p>To this:<\/p>\n<pre><code>$.getJSON(\"http:\/\/www.containernurseries.co.nz\/json\/jsonPlantDetails.php?callback=?\",\n<\/code><\/pre>\n<p>jQuery then would automatically produce a JSONP request for you. It works, but since the result is not executable JavaScript, you get the following error in Chrome:<\/p>\n<blockquote>\n<p>Uncaught SyntaxError: Unexpected token :<\/p>\n<\/blockquote>\n<p>Since I think Chrome is trying to execute the JSON as a function, which it is not.<\/p>\n<p>Changes need to be made to the server, I believe, to get this working cross-domain, if you need that.<\/p>\n<p>Here is a good article on cross-domain issues:<\/p>\n<p>https:\/\/developer.mozilla.org\/en-US\/docs\/HTTP_access_control<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-15 04:22:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>There are several problems I found and wrestled with, but finally got some working code: $.getJSON(&#8220;http:\/\/ip.jsontest.com&#8221;, null, function (data) { console.log(data) }); $(document).ready(function () { ($(&#8216;#a1&#8217;)).mouseover(function (e) { ($(&#8216;#a1&#8217;).attr(&#8216;src&#8217;, &#8216;http:\/\/www.containernurseries.co.nz\/images\/services.gif&#8217;)); }).mouseout(function (e) { ($(&#8216;#a1&#8217;)).attr(&#8216;src&#8217;, &#8216;http:\/\/www.containernurseries.co.nz\/images\/contacticon.gif&#8217;); }); }); You can see it work here: http:\/\/www.sanbarcomputing.com\/flat\/forumPosts\/imgSrcType\/imgSrcTypeNoComments.html This code uses a service called &#8220;JSONTest&#8221; to get properly formatted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1372","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1372"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1372\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}