I have an HTML page (http://localhost:8888/test/test.html) in which I'm trying to pull data from a Drupal 7 website.

I'm using this AJAX code with HTML-Request. I have tried JSON and the website prevents me from pulling, even I have added the following code to pull the data.

<?php header('Access-Control-Allow-Origin: *'); ?>

This is the HTML page that requests AJAX from Drupal 7 website. Could you please check it for me?

<script src="http://code.jquery.com/jquery-latest.js"></script>
  (function ($) {
    $(document).ready(function () {
        // The link we are accessing.
        url: "http://horizon-websolutions.com",
        // The type of request.
        type: "get",
        // The type of data that is getting returned.
        dataType: "html",
        error: function () {
          // Load the content in to the page.
          $("#output").html("<p>Page Not Found!!</p>");
        beforeSend: function () {
          alert("not yet");
        complete: function () {
        success: function (strData) {
          // Load the content in to the page.
      // Prevent default click.
<div id="output"></div>

Please help me telling the website to accept my localhost request to pull data from the server. I have tested the same code on a pure PHP page and it is working fine.

  • What actual error are you getting? Do you see the CORS header in the actual response? – mpdonadio Oct 10 '13 at 18:40

You should wrap you javascript to user $ for jQuery, since it does not automatically refer to jQuery in Drupal 7. Try like that:

(function ($) {
  // Your code here
  $(document).ready(function() {
    // ...

Reference: Managing JavaScript in Drupal 7

  • i have tried your way dear , but with no resutl – Mohammad Moussa Oct 9 '13 at 7:39
  • i have updated the whole code , could you please check it for me ? just add it in an html page an check it , :( – Mohammad Moussa Oct 9 '13 at 7:46
  • I can check it first this evening, since I got to work now. Hope you solve your problem till then. – Елин Й. Oct 9 '13 at 8:09
  • You need to provide more info, where do you put this script in Drupal, what errors you get, etc. – Елин Й. Oct 9 '13 at 19:12
  • no dear , this code is placed in an html page . that connect to a drupal page to pull data , this is it , no more info needed – Mohammad Moussa Oct 10 '13 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.