Consume Web Service Method From HTML5 using Javascript - html

I am developing my first HTML 5 application which has to read data from a web service.
Can anybody tell me how I can call a web method from HTML 5 using JavaScript ?

In this question you have an answer what should you put in order to call a web method in HTML.
How can I call this HelloWorld web method with plain JavaScript/Ajax, without using JSON or anything?
It is using ajax, but it should give you an example. Also take a look at the answer

Related

how to send json file to a restful webservice from html

I recently started learning to create restful web services with spring framework.
all the course is using postman to send requests but I want to send requests from a web page, like creating a table and send firstName and lastName from the HTML file (from the view, .jsp file) and store it in the database.
everywhere I lookt, they all saying the standard file to send to or retrieve from a restful endpoint is JSON, not HTML.
and from what I see #RequestBody only accepts JSON or XML, not HTML inputs.
I tried sending data from HTML(Method = "POST") to a #PostMapping method of my restful web server, to create something and store it in the database but an exception that says "Content-type not supported" was thrown.
I have lots of questions about this, and they all point to the same thing, Not understanding the whole thing.
what's the point of creating restful web service for the back end of a website, when HTML doesn't support put and delete requests and standard file that everyone uses to get requests from the client is JSON, not HTML.
can a web page (HTML) generate it's content to form a JSON file that is being sent back from a restful server?
how can I generate a JSON file from the inputs in HTML file and send it to the restful #PostMapping method?
there are two things that I should mention here,
I don't know much about creating web pages (HTML) I have only been creating very simple HTML files to help me create and test a back end server.
I searched so many questions before I post this one, and none of them helped me.
I will try to answer your questions
First, you need to understand what rest is it is a Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.(https://en.wikipedia.org/wiki/Representational_state_transfer)
so we follow a set of standards to make things easier.
Now coming to your actual question why JSON and not HTML because these are not only consumed from HTML these are consumed by different apps or services and json is a more lightweight and less verbose format, and it’s easier to read and write as well. In most cases, it’s ideal for data interchange over the internet
and we can use it with front end libraries like react , jquery or vanilla javascript to render the content in HTML from JSON without any changes to the API and also use a Backend web service to use this API.
https://api.jquery.com/jQuery.getJSON/
https://reactjs.org/docs/faq-ajax.html
for the other two questions basically You don't have to generate JSON file to call the rest API from HTML and render the content in HTML Instead you can use front end libraries like Jquery, React, Angular or use vanilla javascript to render content and call the rest API.

How to Handle Loading Data Based on Selection in MVC?

I have always worked with Web APIs, so I don't know how to handle this very basic problem in .NET Core MVC (I am only familiar with MVC conceptually). My problem:
I need a user to select an option from a dropdown on the front end and then show some data based on the option (after fetching it).
If I were writing an SPA consuming an API, I would simply make a call to the backend to get the data and then generate the html to display it on the front end.
How is this handled in MVC? Isn't the convention to return entirely new views? How are things like these handled?
I just need a pointer in the right direction conceptually - I can figure out the code.
Edit: Should I just pretend it's an SPA despite it being a view and create an API endpoint in the same app that provides the view and consume it from the cshtml?
It works exactly the same way. You make an AJAX call to fetch some data. You can either return the data directly, and utilize JS to render the HTML or return HTML directly. Either way, you use the AJAX callback to replace the appropriate content on the page.
Even in older ASP.NET MVC projects it worked this way, though you basically had to decide whether you were going to use an MVC controller or a Web Api controller to do the work. Either would work, but there were advantages/disadvantages to each approach. MVC/Web Api could always coexist in the same project.
In ASP.NET Core, the difference is purely semantic. There's really no such thing as MVC and Web Api anymore - just ASP.NET Core. A controller is a controller is a controller, so just add an endpoint and go to town.

How to map JSON object returned from REST controller to html page

I need to know how data returned from the Spring MVC REST controller attaches to html page and what the Spring configuration do we have to do this.
The browser just prints this data out without view and that's all.
I have some Spring configurations to do it but unfortunatelly it doesn't work.
First, you have to create a spring web-mvc application with a correct configuration that will run without errors. There are plenty of tutorials on how to do it. After that, if you wish to get json data using your rest controllers, the most popular way to do it is by using javascript and the JQuery library. Using them you can make ajax calls to your api. There are plenty of tutorials on how to achive this again. Finnaly, your data will be on the client side and you can manage them (render them in your html-dom etc) using javascript and JQuery again.

Mock a web server for ajax in IntelliJ environment

IntelliJ can deploy HTML/JS code at localhost:63342 on the fly. I can see how the frontend behaves immediately.
Now I have a web application.
http://localhost:8080/frontend
http://localhost:8080/backend
The frontend will fetch data from its backend. Now I cannot start a real backend at localhost:63342. How can mock some ajax response at localhost:63342. Is there any magic plugin of IntelliJ can help?
My current solution is put all ajax call in an Angular service module and replace it with a mocked service module. I believe there must be some better solutions.
If you want to mock up some endpoints for you to test with I would suggest you use Mockjax. This will allow you to do exactly what you want and you won't have to run two separate apps to do so. I'm not familiar with IntelliJ, but Mockjax only has a dependency on jQuery and you should be able to easily set it up with any project that at least uses jQuery.

Is it possible to create a ASP classic web service without using SOAP?

I need to create a Web Service in ASP classic(no choice unfortunately) but I need to do it using more modern standards like using JSON instead of SOAP. I'v been searching the web but I can't seem to find any way to do this. Is it even possible? and if it is can you please show me a tutorial or example.
Here are a few resources that may provide some of the information you need:
https://softwareengineering.stackexchange.com/questions/88556/how-to-make-classic-asp-interesting-if-you-are-stuck-with-it
Calling REST web services from a classic asp page
Can I build a REST application using ASP Classic?
ASP Classic example of REST / JSON call: https://gist.github.com/joseph-montanez/1948929
My c# WebService Class has these Compiler Tags:
[WebService(Namespace = "http://myWebService/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
Since it was requested that the WebService can also communicate with JSON clients we added this Compiler Tag:
[ScriptService]
Methods are tagged with
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
It all works fine with SOAP clients and with JSON clients but the
JSON Developers want to get Standard Status Codes in case the
WebService throws an Exception and I don't know how to
accomplish this.