AngularJS HTTP GET request failed to load the specified URL

I am working on a simple Angular code to read JSON data from a specific API URL. When I access the URL , it returns the following JSON data:

Although the URL works fine when accessed through a browser, I'm facing issues while trying to read the same URL using Angular code.

/**
 * Created by ZeroL on 27/05/2017.
 */
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $http) {
    var $seplink = "sepehr360.com/Flight/FlightSearchGrouped?str={"Browser":"TestAndroid","CookieGuid":"","CredentialPassword":"","CredentialUserName":"","CurencyType":"IRR","CurrentUICulture":"fa-ir","From":"SYZ","IntervalDay":0,"IsCancelAvail":false,"IsDirectContract":false,"IsFlightContinuous":false,"IsLogin":true,"IsOnlineSupport":false,"IsTour":false,"Language":"fa","PageSize":10,"Passenger":"","PassengerItem":"","RecordId":1,"SortOrder":1,"StartDate":"1396/3/15","To":"IKA,THR","UserHostAddress":"","UserInfoId":"","UserName":"Mobile360","getTwoWay":false}";
    var $seplink1 = "data.json";
    
    $http.get($seplink)
        .success(function(response){
            $scope.myData = response;
        });
});
<html>
<head>
    <script src="angular.min.js"></script>
    <script src="js.js"></script>

</head>
<body ng-app="myApp" ng-controller="myController">
Search : <input title="search" type="text" value="" ng-model="search"/>

<table border=1>
<tr ng-repeat="data in myData.FlightList">

    <td><img src="http://sepehr360.ir/{{data.AirLineImage}}" alt="ar"></td>
    <td>{{data.AirLineTitle}}</td>
    <td>{{data.FormatedPrice}}</td>

<td>  <a href="http://sepehr360.com{{data.FlightItems[0].FlightInfo.AgencyAddress}}">get this ticket</a>
</td>
</tr>

</table>
</body>
</html>

Despite seeing results without any access info, I have managed to successfully save the URL data to a local file named 'data.js'. The code works perfectly with this setup. Can someone please guide me on how to efficiently read JSON data from the specified URL using HTTP? Your help is greatly appreciated. Thank you!

Answer №1

Have you attempted utilizing JSON.stringify on the JSON data and then adding it to the URL?

Here's an example:

var $link = "simpleURL";

var $jsonData = JSON.stringify(data);

var finalURL = $link + $jsonData;

$http.get($link)
    .success(function(response){
        $scope.data = response;
    }

Additionally, could you provide details about the error message you're encountering?

Answer №2

It is essential to save the information from the response that contains the actual data sent by your server.

$scope.myData = response.data

Answer №3

It seems that there is an issue with the string literal in your code

"sepehr360.com/Flight/FlightSearchGrouped?str={"Browser":"TestAndroid","CookieGuid":"","CredentialPassword":"","CredentialUserName":"","CurencyType":"IRR","CurrentUICulture":"fa-ir","From":"SYZ","IntervalDay":0,"IsCancelAvail":false,"IsDirectContract":false,"IsFlightContinuous":false,"IsLogin":true,"IsOnlineSupport":false,"IsTour":false,"Language":"fa","PageSize":10,"Passenger":"","PassengerItem":"","RecordId":1,"SortOrder":1,"StartDate":"1396/3/15","To":"IKA,THR","UserHostAddress":"","UserInfoId":"","UserName":"Mobile360","getTwoWay":false}"

To fix this issue, you can either use single quotes (') to wrap the literal:

'sepehr360.com/Flight/FlightSearchGrouped?str={"Browser":"TestAndroid","CookieGuid":"","CredentialPassword":"","CredentialUserName":"","CurencyType":"IRR","CurrentUICulture":"fa-ir","From":"SYZ","IntervalDay":0,"IsCancelAvail":false,"IsDirectContract":false,"IsFlightContinuous":false,"IsLogin":true,"IsOnlineSupport":false,"IsTour":false,"Language":"fa","PageSize":10,"Passenger":"","PassengerItem":"","RecordId":1,"SortOrder":1,"StartDate":"1396/3/15","To":"IKA,THR","UserHostAddress":"","UserInfoId":"","UserName":"Mobile360","getTwoWay":false}'

Alternatively, you can escape the double quotes (") that are within the string like so:

"sepehr360.com/Flight/FlightSearchGrouped?str={\"Browser\":\"TestAndroid\"......"

Furthermore, the JavaScript code provided appears to be invalid as it contains two closing curly brackets (}}) after the controller declaration that are not correctly placed.

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

Vue 3 array filtering does not result in a new array being generated

I'm a bit confused about the behavior in Vue 3 when filtering a reactive array to create a new array and then modifying the new array, why does it update the original one? Shouldn't the filter operation generate a completely separate array with n ...

What is the best way to loop through an array in JavaScript, calling a prototype function recursively within itself?

I am currently working on developing a custom polyfill for the Array.flat() method. However, I have encountered some challenges when trying to call the function recursively within itself to flatten nested arrays further. It seems that when I write code wit ...

Trying to use the `await` keyword results in a "SyntaxError: Unexpted identifier" message, even when used within an `async` function

Encountering an error with Javascript's await when using it inside an async module let ImagesArray = await getImages(); ^^^^^^^^^ SyntaxError: Unexpected identifier at createScript (vm.js:80:10) at Object.runInThis ...

Retrieve the address information from the nearby JSON file

Looking to extract data from a local JSON file with the following structure: JSON format: {"":[{"name":"","phone":"","address":"","landmark":"","mainarea":""} The code snippet: func loadAllContacts(){ let cityCode = UserDefaults.standard.value( ...

Make sure to add the node_modules/ directory before pushing to the gh-pages branch

Struggling to deploy my site on the gh-pages branch of Github... I must include the node_modules/ folder, but git won't let me add it for committing changes. I know it's intentional not to include dependencies during development, but for live dep ...

Dealing with a JSON array in PHP that has been transferred through jQuery

Currently, I am facing a challenge with consuming a JSON array in PHP that is sent via JQuery. The data structure being transmitted to the server is as follows: [{"id":"7","start":"00:00","end":"02:30","date":"2013-11-15"},{"id":"10","start":"23:00","end" ...

The markers within a loop in react-native-maps are failing to render

Recently, I've been delving into the world of React Native app development for iOS. Specifically, I've been experimenting with the react-native-maps package. Here's the issue I'm facing: When I statically render a single MapView.Marker, ...

I need to mass upload a collection of resumes stored in a zip file, then extract and display the content of each resume using a combination of HTML

I recently used a service to extract and retrieve the contents of a zip file. I am trying to read the content of the files and integrate them into the scope of my Angular project. Any suggestions would be greatly appreciated. Below is an outline of my func ...

Retrieve active route information from another component

We are utilizing a component (ka-cockpit-panel) that is not linked to any route and manually inserted into another component like so: .. ... <section class="ka-cockpit-panel cockpit-1 pull-left"> <ka-cockpit-panel></ka- ...

Unravel complex JSON arrays in PHP with multiple dimensions

I am struggling to decode a JSON array and make it readable without receiving an error. My desired output should look like this: image1 image2 image3 $json = ' { "acf" : { "hero_banner" : [ { "banner_image" : ...

What are the optimal scenarios for utilizing angular $q and how should it be

I've been trying to wrap my head around the functionality of Angular's $q, but it's just not clicking for me. Can someone explain when and how $q should be utilized in Angular? ...

Clicking on AngularJS ng-click to navigate to a different page within an Ionic framework application

My goal is to navigate to another page when clicking on a button in the Ionic navbar at the top. However, I am encountering an issue where upon clicking, all nav bar buttons disappear. Interestingly, using dummy codes triggers an alert successfully. But w ...

Is it possible to use AJAX in JavaScript to dynamically update page titles?

When loading a new page using AJAX, I am having trouble updating the title. The expected titles should be "Home", "About", and "FAQ". I attempted to solve this by changing the title with the code snippet below, but it only resulted in displaying "undefine ...

Present information incrementally by showcasing each individual object element through knockout

I am looking to create a simple table layout that will allow me to showcase data from an array of objects one by one. By clicking on a button or a similar element, the next object in the array should be displayed. What makes this challenge interesting is ...

How to access selection range styles using JavaScript

It is common knowledge that we can retrieve the selection of text in JavaScript using the following method: var range = window.getSelection (); However, how can we obtain the style of this selection? For example, when I select bolded text or italicized ...

`Adjusting video frame on canvas`

When I capture video from a user's camera and display it in a 1:1 square, I encounter an issue when trying to draw the picture to a canvas. The image is not drawn in the same location on the canvas. How can I resolve this problem? To see a live examp ...

Exceeded the maximum stack size limit

let newArray = []; $('#select_students option:selected, #cc_select_students option:selected').each(function(){ newArray.push(item); }); ...

I need to convert the current webpage into a canvas using Javascript in order to manipulate its elements

Is it possible to transform an HTML page into a canvas for graphical manipulation? I am interested in creating a 3D effect that makes the page appear to wrap around the user's head. Do you have any suggestions on how to achieve this? ...

What is the best approach to storing and retrieving special characters ('+<>$") from a textfield into a database using PHP?

I have a form where users can enter a personal message with a subject. The data entered in the textarea is passed to a Javascript/jQuery function, which then sends it to a PHP file for storage in a database. However, I am encountering issues when special c ...

What is the best way to check if a function has been successfully executed?

When working with PDF documents, I often use an instance of pdfkit document known as doc: import PDFDocument from 'pdfkit' const doc = new PDFDocument() This doc instance is then passed into a function called outputTitle: export const outputTi ...