0

I'm calling an API on button click which returns me the response in JSON format.
My code for calling API is as follows:-

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="dropdown">
    <span class="list">
        <select id="itemList">
        <option value="select">Select</option>
        @foreach (var item in Model)
        {
            <option value="@item">@item</option>
        }
        </select>
    </span>
    <span class="button">
        <button type="button" id="btn">Watermark</button>
        <script type="text/javascript">
            $(function () {
                $("#btn").bind("click", function () {
                    var url = "http://104.211.243.204/?container=comedy-movies&blob=" + encodeURIComponent($("#itemList").val());
                    window.location.href = url;
                });
            });
        </script>
    </span >
</div >

Now after execution it redirects me to the url and on that window it shows me the output. Now i want to use that output in another logic. So i want to store that json response in client side into a variable without redirecting me to that url. Please help me regarding this.

This is the output from API:-

{
  "url": "https://moviestarstorage.blob.core.windows.net/water/gol.mp4"
}

Now i want to store this result into a variable at client side and used that variable to video tag. API returns me the result of video. Please help me regarding this. I'm using the c# Model View Controller for this
Please help me regarding this. I have search for many solutions but no one work for me . So if there is any way to do this please help me regarding that. Waiting for positive reply.

Igor
  • 3,054
  • 1
  • 22
  • 28
  • `window.location.href = url;` this redirects to the url so you should not be using it. Better use the video tag in partial view and then reload/refresh it with passing the json result – Arijit Mukherjee Mar 25 '18 at 09:52
  • remove redirection, make use of AJAX call and do whatever you want with the data from your JSON. – M. Adeel Khalid Mar 25 '18 at 09:54
  • sorry but i didnt get you sir – Abhijeet Panpatil Mar 25 '18 at 09:55
  • https://stackoverflow.com/questions/14419436/reloading-partial-view-with-jquery Check this answer – Arijit Mukherjee Mar 25 '18 at 09:55
  • @M Adeel Khalid sir How to perform ajax call based on my code?? Please guide me , Thank You. – Abhijeet Panpatil Mar 25 '18 at 09:56
  • @AbhijeetPanpatil, what is your goal? you want to display that video? If you simply want to hold the value of the variable `url` as you claim on the title then just define the variable outside `$(function () { ... }` and then update the value where you want. Otherwise it's not going to be accesible from other places. – derloopkat Mar 25 '18 at 10:18
  • I want to display that video in my view page. json gives the url og video. So i want to store that video url at client side. and display in html – Abhijeet Panpatil Mar 25 '18 at 10:25
  • so i can take that variable which holds the url of video and im pass it to the video tag . – Abhijeet Panpatil Mar 25 '18 at 10:33
  • If your web site is in a different domain you probably are getting CORS error when calling that API. – derloopkat Mar 25 '18 at 12:06
  • yeah sir i getting the error like below:-Failed to load http://104.211.243.204/?container=comedy-movies&blob=come2.mp4: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50136' is therefore not allowed access. now what is the solution for this? – Abhijeet Panpatil Mar 25 '18 at 12:41
  • Try adding cross origin header in the request. If it doesn't work this means the API does not want to be accessed from other domains. However you can use reverse proxy as a workaround. I've answered a similar question previously. https://stackoverflow.com/a/47316607/2516718 Check out those 3 links in my answer. – derloopkat Mar 26 '18 at 08:23

0 Answers0