What is the method for accessing cookies using JSONP?

Hello, I am trying to load a page from index.html using the following code:

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script>
<script>
function jsonCallback(json){
  console.log(json);
  alert(document.cookie.json)
}

$.ajax({
  url: "https://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json",
  dataType: "jsonp"
});
</script>

I am attempting to retrieve cookies by executing document.cookie='test=123' in the Console of , but it returns undefined. Why is this happening and how can I resolve it?

Below is the content that URL returns:

jsonCallback(
  {
    "sites":
    [
      {
        "siteName": "SitePoint",
        "domainName": "https://www.sitepoint.com",
        "description": "SitePoint is a hub for web developers to share their passion for building incredible Internet things."
      },
      {
        "siteName": "A List Apart",
        "domainName": "http://alistapart.com/",
        "description": "A List Apart explores the design, development, and meaning of web content, with a special focus on web standards and best practices."
      },
      {
        "siteName": "Smashing Magazine",
        "domainName": "https://www.smashingmagazine.com/",
        "description": "Smashing Magazine delivers useful and innovative information to Web designers and developers."
      }
    ]
  }
);

Answer №1

It is not possible to use JavaScript code within your webpage to access cookies from different origins. For instance, if you have a page located at http://example.com/foo.html and you include a script (such as JSONP) from http://api.com/, any cookies that are sent back in the response from http://api.com/ will be associated with http://api.com/. Therefore, it is not feasible to retrieve these cookies using JavaScript on the page located at http://example.com/foo.html.

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

Assign a predetermined value to a dropdown list within a FormGroup

I have received 2 sets of data from my API: { "content": [{ "id": 1, "roleName": "admin", }, { "id": 2, "roleName": "user", }, { "id": 3, "roleName": "other", } ], "last": true, "totalEleme ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

Using the built-in http module in node.js to upload images via multipart/form-data

I have encountered an issue where I need to send two images and an API key as part of a multipart/form-data HTTP request to an API. The images are retrieved from an AWS S3 bucket, which works fine. However, when attempting to send the image data as part ...

The Ajax request functions flawlessly on Mozilla but encounters issues on Chrome, causing confusion as it occasionally works

I am using a PHP file with a class and function to store data in the database, accessed via AJAX. While everything works smoothly in Mozilla, Chrome seems to be causing some issues. Strangely, sometimes it works fine, but other times it fails for no appare ...

How can you use ES6 pure Javascript to set the src attribute of an html collection of images from an array of paths as values for each image?

Currently, I have an array filled with different src values like [path1, path2, path3], and a corresponding HTML collection which has the same number of elements as the array. I am looking for a way to assign each value from the array to set the src attrib ...

How can you utilize jq to retrieve values from a JSON array that have a specific key with a boolean value of true

Here is a JSON data structure: [ { 'id': 'something', 'isSparse': true }, ... ] I'm looking to extract the IDs of entries where isSparse == true using a jq command. How can I achieve this? I attempted the ...

Ways to ensure that the form data stays consistent and visible to the user continuously

Is there a way to ensure that the user submits the form, stores it in a MYSQL database, and then displays the same submitted data in the form field? <div class="form-group"> <label class="control-label col-sm-2" for="lname">Last Name*</l ...

Unable to bring in the Firebase Firestore Colletion

When working on my next app, I encountered an error while trying to call a Firestore Collection. The specific error message that appeared when I ran the app was: FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicat ...

Encountering a Javascript Error when trying to begin

As a beginner in Javascript, I am venturing into designing a simple website that incorporates Javascript. Currently, my focus is on building a calculator. However, upon loading my website, nothing seems to initiate and there are no error messages displayed ...

Providing dynamic string outputs depending on the current date using AngularJS

I set up an advent calendar that reveals a new question every day, but currently the questions aren't showing up. Here's the code I have: The controller located in app.js: .controller('textCtrl', function($http) { this.data = {}; ...

Server side progress bar with Jquery

I am currently dealing with a lengthy server-side process that is not fixed in terms of time and involves running multiple queries in the database. This process takes more than 30 seconds to complete. I am looking for a way to show the progress in percen ...

What could be the reason for the selection box in my form not changing the items when togg

I'm having an issue with my form selection box code that used to work but is now not functioning properly. Could someone please help me identify where the error lies? Essentially, I have a form with the ID #main and a select box named #chart-type. Th ...

Angular directive preventing default action but Chrome still loading image on drag

Has anyone encountered an issue with an angular directive that is not successfully preventing Chrome's default action? Below is the code for the directive in question: app.directive('fileDrag', function () { return { restrict: ' ...

Retrieve values from a database table in Laravel by comparing the IDs stored in one table in JSON format with the IDs in another table

My database table is called journal_details and it contains a column named transaction_by_to. I store values of account_head_id in JSON format within the transaction_by_to column of the journal_details table. Now, I need to execute a select query to retrie ...

Is jest the ideal tool for testing an Angular Library?

I am currently testing an Angular 9 library using Jest. I have added the necessary dependencies for Jest and Typescript in my local library's package.json as shown below: "devDependencies": { "@types/jest": "^25.1.3", "jest": "^25.1.0", ...

Interaction between Javascript and PHP is only triggered upon the second click

Despite my efforts to find a solution, I've been facing some challenges... I'm currently developing a mobile hybrid app. The user is required to input their ID number, which is then sent to a javascript function for validation. Following this, a ...

Having difficulty populating a selection box through an ajax request in Django

I am facing an issue with creating cascading select boxes in my project (backend Django), although I believe most of the backend work has been completed. The JavaScript code I'm using is adapted from a solution found on a stackoverflow post. $(docume ...

What method can I utilize in Powershell to filter out specific key names from a JSON file?

Attempting to shrink a large 700MB JSON file, which is a scaled-down version of the file found here: The goal is to eliminate unnecessary data by excluding certain keys such as hypernyms, pos, categories, and more. However, previous attempts at this using ...

Ways to retrieve json_decode() information following ajax transmission

Currently experimenting with an ajax script to enhance my skills in ajax. I am using the serializeArray() method to submit a form via ajax, however, although I can successfully pass the data, I am unable to perform any operations on it. var submit_post = ...

If the server goes offline, it will not produce an error message

Running a factory in AngularJS angular.module('app.services',[]) .factory('myFactory', function($http){ return { getData: function(){ return {animal: 'dog'} }, isUser: function() ...