13

I Need to do cross domain Ajax request - Here is my code

 $.ajax(
        {
            url: redirectURL,
            data: $('#login-container form').serialize() + querystring,
            type: 'post',
            cache: false,
            dataType: 'jsonp',
            jsonp: 'jsonp_callback',
 });

Error: [Exception... "Access to restricted URI denied"  code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)"  location: "http://testsite/assets/scripts/jquery-1.3.2.js Line: 19"]
Source File: http://testsite/assets/scripts/jquery-1.3.2.js
Line: 19

I have checkout the following links too -

Access to restricted URI denied code: 1012

 $.ajax(
        {
   url: redirectURL+'?callback=?',
            data: $('#login-container form').serialize() + querystring,
            type: 'post',
            cache: false,
            dataType: 'html' });

I have tried Callback in url too . I had already seen all link in stackoverflow regarding this issue.. but not able to overcome this thing Can anyone please help and tell me how to overcome !! Thanks

3
  • 1
    What does the url look like and more important: does it really return jsonp data?
    – jitter
    Nov 5, 2009 at 16:18
  • Do you control the domain/site you are making the ajax request to? Is it set up with a json response, or a jsonp one?
    – Stobor
    Dec 20, 2009 at 0:51
  • And do you have a javascript function called jsonp_callback defined in your page?
    – Stobor
    Dec 20, 2009 at 0:54

6 Answers 6

30

You won't be able to do a cross-domain POST request in the browser.

If you are making a JSONP call to access a cross-domain URL, you can use JQuery's getJSON method. This would allow you to make a GET request only. If you can submit your login information to the redirectURL using GET parameters, you could make this work.

Note that POSTing to remote login forms is perhaps the best example of why browsers disallow cross-domain requests like this. You don't want a page that looks like your bank to be able to actually serve you data from your bank's website -- that would make a very effective phishing page.

On the other hand, if you really want to work around this, you can write some server-side code that, given the input parameters, makes a post request to the redirectURL and funnels back the response.

Please tell me you are not writing a phishing page.

2
  • 1
    funny comment. But sometime it happens to be so that you have to do it when your client's GoDaddy QSC (quick shopping cart) is all set up and from the public landing/presence pages your client wants some authenticating mechanism to know if the registered user has enough privileges so that you can show him/her the Animated Version of your Subscriber-Only-Magazine flash-based version hosted on a service like http://ISSUE.com, rather than the old-styled-Download-PDF-button. !!! Apr 15, 2014 at 9:54
  • 1
    this is no longer true; you can make POST requests to servers which allow this with the appropriate CORS headers.
    – oberhamsi
    Jul 4, 2014 at 8:52
7
+50

One way to do it is to use your server as a proxy:

AJAX Request          -> Your server -> Server of interest

AJAX Response Handler <- Your server <- Server of interest 
3

JSONP is not really Ajax in a way that it does not use XMLHttpRequest but a dynamic script element. jQuery makes this transparent but JSONP still requires the following to work:

  1. HTTP GET only, you can't use POSTs
  2. the server needs to support it by wrapping the response in a call to the callback you specify

Check if the URL you request supports JSONP and change the call to an HTTP GET.

2

If you do control the remote server, too, in recent browsers (IE8, FF3.5) you can overcome the cross-domain obstacle:

IE's XDomainRequest method and the W3C's and Mozillas Origin-Header.

In short, if your server sends back the HTTP header

Access-Control-Allow-Origin: *

with the AJAX response, IE and Mozilla will accept it, even if the request stems from a different domain. You can narrow down allowed requesting domains by replacing the * with them.

0

"I need to do cross domain Ajax request"

You can't do that, unless:

  • you are using grease monkey
  • you are making a jsonp request which isnt really ajax
2
  • and guess what. if you look at his code he does a jsonp request
    – jitter
    Nov 5, 2009 at 16:16
  • So could you please suggest a way by which i can use $.ajax in order to access my redirectURL Nov 5, 2009 at 16:17
0

run it on any server like wamp or apache...

it dont gives an error...

NS_ERROR_DOM_BAD_URI: Access to restricted URI 

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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