# Launch the function loadJSON() with a html button

**URL:** <https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802>\
**Category:** Beginners\
**Created:** [August 20, 2018, 8:35pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802 "2018-08-20T20:35:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 20, 2018, 8:35pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/1 "2018-08-20T20:35:21Z")

</div>

Hello,  
I would like to launch a loadJSON() function with an html input element.  
below is my code, it should work but I have this error : _NetworkError when attempting to fetch resource_ …  
how to resolve it ?

```auto
<!DOCTYPE html>
<head>
	<meta charset=utf-8>
	<meta name=viewport content=width=device-width,initial-scale=1>
	<script defer src=https://cdn.JsDelivr.net/npm/p5></script>
	<script defer src=https://cdn.JsDelivr.net/npm/p5/lib/addons/p5.dom.min.js>	</script>

<script type="text/javascript">
		function setup(){
			noCanvas(), noLoop();				
		}

		function myfunction(){
			var url = 'https://api.archives-ouvertes.fr/search/?q=title_t:(javascript)&wt=json';
			loadJSON(url, treatData);
		}
		
		function treatData(jsonData){
			var data = jsonData.response.numFound;
			print(data);
		}
	</script>
</head>
<body>
	<form> 
		<input type="submit" value="Submit" onclick="myfunction()">
	</form>
</body>

```

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 21, 2018, 9:27am UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/2 "2018-08-21T09:27:41Z")

</div>

With an html button, I manage to launch a function with some text (cf [this post](https://discourse.processing.org/t/accessing-html-elements-defined-into-html-file-solved/2701/3?u=mloiz)), but I dont manage to launch a function with loadJSON() 😕

maybe the problem is related to this _callback function_ of [loadJSON()](https://p5js.org/reference/#/p5/loadJSON)❓

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 21, 2018, 4:17pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/3 "2018-08-21T16:17:03Z")

</div>

What is the exact text of your error? Can you call `myfunction` from something other than an `onclick` callback? Can you visit the URL directly?

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 21, 2018, 6:18pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/4 "2018-08-21T18:18:39Z")

</div>

The exact error is _TypeError: “NetworkError when attempting to fetch resource.”_

I dont want to visit the URL directly – like with `function preload()` – because my goal is after to add an input Html element where the user can add some text, which is then used to build the URL…

In the upper example, URL is `api.archives-ouvertes.fr/search/?q=title_t:(javascript)&wt=json' `  
and my goal is that the `javascript` string could be replaced by some text typed from the user.

> Can you call `myfunction` from something other than an `onclick` callback?

I have to search and test.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 21, 2018, 6:23pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/5 "2018-08-21T18:23:06Z")

</div>

> [@mloiz](#):
>
> I dont want to visit the URL directly

Yeah, I know this isn’t your end goal. But when you encounter an error, often you’ll have to troubleshoot and isolate the error. If I was getting a network error, I’d try to visit the URL directly. If I can do that, then I know the URL is okay and the error is elsewhere. If I’m unable to visit the URL directly, then I’d know that maybe the URL was incorrect or the server was down or something.

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 21, 2018, 7:42pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/6 "2018-08-21T19:42:33Z")

</div>

I see…  
the URL, and my code, works 🙂 ; I have tried them with the [preload function](https://p5js.org/reference/#/p5/loadJSON) :

```auto
	        var jsonData;

		function preload(){
			var url = 'https://api.archives-ouvertes.fr/search/?q=title_t:(javascript)&wt=json';
			jsonData = loadJSON(url);
		}

		function setup(){
			noCanvas(), noLoop();				
		}

		function draw(){
			var data = jsonData.response.numFound;
			var feedback ; 
			if(data != 0) feedback = 'the URL works';
			else feedback = 'the URL did not work';
			createP(feedback);
		}
		

```

maybe the problem is in the articulation of functions. In the reference examples, the URL can be load in the `preload function`, or in the `setup function`, but is it possible to `loadJSON()` in a `draw` function ?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 21, 2018, 8:09pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/7 "2018-08-21T20:09:17Z")

</div>

> [@mloiz](#):
>
> `loadJSON()` in a `draw` function

Doing this is a bad idea as `draw()` is executed at least 30 times per second. You could attach it to an event associated to an element in your DOM. For example, when a mouse is clicked or a key is typed in a field. Is that what you mean?

In your first code, I vbelieve those fields in the header need to be placed within quotation marks. If you check your console output, you should get some errors indicating you need to fix those fields. Did you see those messages?

Regarding your url, as Kevin mentioned, it is important that you get a response when you load that request directly. I wasn’t able to get it to work. I didn’t know if it was because the request wa ill-formulated or there was an error in the service. But it seems you got it to work, so that is good for the time being. It is important to have a runnable example here in the forum to be able to reproduce the problems and solve questions efficiently.

Kf

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 21, 2018, 8:50pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/8 "2018-08-21T20:50:03Z")

</div>

> [@kfrajer](#):
>
> In your first code, I vbelieve those fields in the header need to be placed within quotation marks. If you check your console output, you should get some errors indicating you need to fix those fields. Did you see those messages?

Oh, no, no errors. I took thoses lines from @GoToLoop[Code 800A0404 issue - #3 by GoToLoop](https://discourse.processing.org/t/code-800a0404-issue/2036/3)

> [@kfrajer](#):
>
> I wasn’t able to get it to work. […] But it seems you got it to work,

yes, I verify my URLs before posting. below is the result.

 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/af072abf4da8cc53222dbcd7efe6c49c80cf1cdd.png)  
I really didn’t know you getting nothing, sorry for that… I understand know Kevin’s answer…

> [@kfrajer](#):
>
> You could attach it to an event associated to an element in your DOM. For example, when a mouse is clicked or a key is typed in a field. Is that what you mean?

I see… do you have an example ?  
so launching a P5JS function with `<input onclick=''\>` HTML is not recommanded/usual ?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 21, 2018, 10:42pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/9 "2018-08-21T22:42:22Z")

</div>

> [@mloiz](#):
>
> so launching a P5JS function with `<input onclick=''\>` HTML is not recommanded/usual ?

You can do that but it is preferable to do it in `setup()`. The idea is not to call that line in `draw()` as you would be doing it many times per second.

Kf

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 22, 2018, 7:58am UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/10 "2018-08-22T07:58:34Z")

</div>

this code launch the function with an event associated to a DOM element

```auto
        var button;

        function setup(){
            noCanvas(), noLoop();

            button = createButton('run function');
            button.position(15,10);
            button.mouseClicked(myFunction);       
        }

        function myFunction(){
            var url = 'https://api.archives-ouvertes.fr/search/?q=title_t:(javascript)&wt=json';
            loadJSON(url, treatData);
        }
       
        function treatData(jsonData){
            var data = jsonData.response.numFound;
            print(data);
        }

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 22, 2018, 2:18pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/11 "2018-08-22T14:18:00Z")

</div>

> [@mloiz](#):
>
> but is it possible to `loadJSON()` in a `draw` function

I was referring to your question. The code above seems fine. Is everything working now?

Kf

---

<div class="post-metadata">

**Author:** ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)\
**Post date:** [August 22, 2018, 3:26pm UTC](https://discourse.processing.org/t/launch-the-function-loadjson-with-a-html-button/2802/12 "2018-08-22T15:26:16Z")

</div>

yes, thanks,  
I have now to retrieve the data from an input element.
