3

When I try to do a ajax call, I got the error on chrome below.

XMLHttpRequest cannot load javascript:;. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Here is code:

$.ajax({
    type: "POST",
    data: {pvalue : pid},
    cache: false,
    url: "xxx.in/yy/ajax.php",
    success: function(data)
    {
      $modal.find('.edit-content').html(data);
    }
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Durgesh Pandey
  • 119
  • 1
  • 1
  • 11
  • is your ajax.php inside your project or you are calling outside project page ? – Ameya Deshpande Sep 15 '15 at 05:59
  • 2
    try without `http:` directly provide directory and page name – Ameya Deshpande Sep 15 '15 at 06:02
  • I have changed it , but I got the same error @AmeyDeshpande – Durgesh Pandey Sep 15 '15 at 06:03
  • 1
    share your path to the file you are accessing from that call – Ameya Deshpande Sep 15 '15 at 06:03
  • There's something you're not showing us about your actual URL for the ajax request. The error msg makes it sound like the URL is not actually of the form `http://somedomain.com`, – jfriend00 Sep 15 '15 at 06:10
  • url: "http://xxx.in/yy/ajax.php", – Durgesh Pandey Sep 15 '15 at 06:11
  • Here's a guess. Is the web page itself that the script is in running on a localhost: or file: URL? – jfriend00 Sep 15 '15 at 06:15
  • @jfriend00 it is in the same project , same domain , same folder and same server – Durgesh Pandey Sep 15 '15 at 06:28
  • 1
    That's not what I'm asking. When you get this error, what is the EXACT URL in the URL bar of the browser. The error says that the URL is something unusual that does not permit cross-origin requests. You need to show us what both the full URL of the page is and the full URL of the ajax call. The browser is complaining about the match between these two so withholding that info from us will not help anyone give you an answer. – jfriend00 Sep 15 '15 at 06:34
  • Why are you not providing the info asked for in the above comment? The error implies that your situation has something to do with the two URLs (page URL and ajax URL). We can't do much more to help without seeing those actual URLs. – jfriend00 Sep 15 '15 at 20:56

2 Answers2

5

All research into that specific error message suggests that the host web page is not being loading via an http:// URL and is probably a file: URL. The browser will not, by default, permit cross origin requests from a file: URL.

You need to load the web page through your web server, not via the file system if you want to use ajax requests.

Here are some other questions and answers about that specific error that all point to the wrong type of URL being used to load the page.

"Cross origin requests are only supported for HTTP." error when loading a local file

React.js: Example in tutorial not working

Cross origin requests are only supported for HTTP but it's not cross-domain

http://answers.playcanvas.com/questions/833/cannot-load-model-due-to-cross-origin-request-being-blocked

https://groups.google.com/forum/#!topic/tincr-for-chrome-devtools/nA9k2qh7F-g

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

If you are accessing data from other domains, you have to override Chrome's Same-origin Policy. For that you have to specify dataType: 'jsonp'

$.ajax({
    type: "POST",
    dataType: 'jsonp',
    data: {pvalue : pid},
    cache: false,
    url: "xxx.in/yy/ajax.php",
    success: function(data)
    {
      $modal.find('.edit-content').html(data);
    }
});

If the file ajax.php in your server (the one which you are working now), you can simply specify the file name in url section (as below).

$.ajax({
    type: "POST",
    data: {pvalue : pid},
    cache: false,
    url: "ajax.php",
    success: function(data)
    {
      $modal.find('.edit-content').html(data);
    }
});
Tismon Varghese
  • 849
  • 1
  • 6
  • 17
  • 1
    This issue does not align with the specific error message the OP is getting. This is not a generic cross origin issue, it's something different than generic. – jfriend00 Sep 15 '15 at 06:39