Angular API call unsuccessful in retrieving data

I am facing an issue fetching medicine information from TrueMD using the API. I have been trying to access it from my localhost, but despite including 'Access-Control-Allow-Origin' as null for web service security reasons, I am still encountering a security error. Is there something crucial that I might be overlooking?

    $scope.medicines = [];
    var search = "crocin";
    var api_key = "API_KEY";

    $http.get("http://www.truemd.in/api/medicine_suggestions?id=" + search + "?key=" + api_key)
    .then(function(response){ 
        response.header('Access-Control-Allow-Origin', "null");
        $scope.medicines = response.data; 
        console.log($scope.medicine);
    });

Answer №1

The reason for the error is due to an invalid GET request format. When using multiple arguments, make sure to separate them with '&'.

Here is how your request should look like: ""

To fix this issue, adjust your code as follows:

$http.get("http://www.truemd.in/api/medicine_suggestions?id=" + search + "&key=" + api_key)
.then(function(response){
    ...
}

Note: The "Access-Control-Allow-Origin" is a server-side header and setting it in the request will not have any effect. You can remove it from your code.

Additionally, attempting to modify the headers of the server response does not serve any purpose. It seems there may be a misunderstanding about how this process works :(

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance the progression bar using JavaScript

Is there a way to dynamically update a progress bar in HTML while a function is running? function fillProgressBar() { var totalIterations = 100; for (var i = 1; i <= totalIterations; i++) { //perform calculations progressBarWidth = (i/to ...

Obtaining slider images using XML in ColdFusion

I've been attempting to incorporate a slider into my ColdFusion page by using a jQuery script that fetches images from an XML file. The XML file contains paths and other image details, but unfortunately, the slider isn't functioning as expected. ...

Setting up Npm Sequelize Package Installation

Could use some help with setting up Sequelize. Here's the current status: https://i.sstatic.net/MQH1I.jpg ...

Is there a way to dynamically insert a <span></span> tag into every line of a <pre></pre> block in HTML without manually coding each line?

One of my recent challenges was figuring out how to incorporate line numbers into source code using CSS. I finally achieved the desired effect, as shown below: https://i.sstatic.net/A0P12.png However, achieving this required me to consistently use <sp ...

The ES6 Vue and Webpack project functions smoothly on Chrome, yet encounters compatibility issues on IE11

Working on a project using ES6, Vue, and Webpack. The project is successfully running in Chrome, but encountering issues when trying to run it in IE11. An error is being thrown with the following message. Any assistance would be greatly appreciated. https ...

Maintain course focus when updating

I'm currently working on a to-do list where clicking on an item adds the "checked" class. However, when I refresh the page, everything reverts back to its original state without the checked class. How can I maintain the state of the checked items? I& ...

What is the best way to retrieve data from a URL and store it in a binary file in C#.NET without dealing with encoding issues?

Looking to retrieve data from a URL in C#.NET and save it as binary in a file. While HttpWebRequest/Streamreader works well for fetching and saving data as ASCII, non-ASCII characters can be problematic due to encoding issues. What's the simplest way ...

Lodash provides an array of values when the specified path is verified as valid

When using Node.js with the following object and lodash "queries" in the terminal, I encounter the following: var obj = { a: [{ b: [{ c: "apple" }, { d: "not apple" }, { c: "pineapple" }] }] }; > _.get(obj ...

Issue with displaying and hiding list elements using jQuery

I am attempting to create an accordion feature using <li> elements with classes .level1, .level2, .level3, and so on. The problem I am encountering is that when I click on a .level2 element, the items hide correctly until the next .level2 element wit ...

Finding the length of child elements outside of a nested ng-repeat in AngularJS

I am facing an issue with a nested ng-repeat. I am trying to access the length of the filtered nested ng-repeat outside the parent ng-repeat, but it always returns 0. If there is only a single ng-repeat, I can retrieve the correct result. I have attempted ...

What is the best way to pass a variable using the href tag in jQuery?

for (var i in result) { $('#tgt').append( "<li><a href='/UpdateStatusData/?id='"+result[i]+" >"+result[i]+"</a></li>"); } I am facing an issue where the id is appearing blank when being extracted o ...

Can you explain the variance between a JavaScript Array and Object besides their .length property?

I have a theory that a JavaScript array functions as a hashmap, only accepting integral values as keys. Also, the .length property simply returns the largest index + 1. Is my understanding correct? Are there any other distinctions to note? ...

Issues with JQuery script causing inconsistency in checking checkboxes

My code includes two functions designed to check and uncheck all checkboxes with a specific class. Initially, the functions work as expected but upon subsequent attempts to run them, the checkboxes do not function properly. Instead, the HTML code seems to ...

Experience the innovative feature of React Splide Carousel where a peek of the next image is shown until you reach

My current challenge arises when I reach the last slide in the slider. I am trying to prevent it from looping and instead stop with no extra space or any other images peeking out. To address this, I have utilized the padding: '5%' option, which ...

The task of renaming a file in javascript when it already exists by incrementing its name like file_1.txt, file_2.txt, and so on is proving to be

After trying out this code snippet, I noticed that it creates a file like file.txt as file_1.txt. However, when I try to use the same filename again, it still shows up as file_1.txt instead of incrementing the number. Is there a way to automatically incr ...

A guide on extracting certain fields and values from a JSON structure

I received a JSON output from the response body that contains various details about a contract. My goal is to extract only two specific pieces of information: "contractId" "contractStatus" To achieve this, I'm utilizing JavaScri ...

Controversial discussions within the $where function in JavaScript

Here is a code snippet to consider: <?php $m = new MongoClient(); $db = $m->selectDB('test'); $collection = new MongoCollection($db, 'phpmanual'); $js = "function() { return this.name == 'Joe' || this.age == 50; } ...

Dividing the JSON dataset into smaller segments

Here is an example demonstrating how to split the JSON data: var data = [ { BankName: 'SBI', IFSC: 'SBIN0002688' }, { BankName: 'ICICI', IFSC: 'ICIC0003931', MICR: '500229094'}, { BankName: 'RBI ...

How to interact with objects/methods in React/Three.js from outside of useEffect

I'm looking to include controls for manipulating the rubiks object, which is created in useEffect. These controls consist of a series of buttons that should trigger functions within the cube class (to which rubiks belongs) stored in a separate compone ...

How can we stop the brief display of a hidden div from occurring?

I have two divs on my webpage - one to display if JavaScript is disabled, and another if JavaScript is enabled. The issue I am facing is that even when JavaScript is not disabled, the div containing the message stating that JavaScript is disabled briefly ...