0

I've tried many ways to access a websites ajax function. Here is the code:

$.ajax({
    type: 'POST',
    url: 'sched', 
    so on and so fourth..
});

Notice: In the url, there is a url parameter named sched. Obviously if i run this in my own server or website this won't work. Is there any way that i may know the root address of this url? Like example.com/sched? Thanks

Subedi Kishor
  • 5,906
  • 5
  • 35
  • 53
  • If there's no domain in the URL, it's the same domain as the page running the script. – Barmar Mar 31 '13 at 08:34
  • The AJAX same-domain policy will probably prevent you from accessing it directly from your own domain. – Barmar Mar 31 '13 at 08:35
  • ive actually tried that. but none of what ive done worked. im in http://assessment.usc.edu.ph/DisplayAssessment/Index?studId=09302424&mode=0 page. do you have any idea how to access the 'sched' file? – Kevin Yee Mar 31 '13 at 08:45
  • ive also tried assessment.usc.edu.ph/DisplayAssessment/sched and it exists but its not the one that the code really refers because ive tried using it and its not working. – Kevin Yee Mar 31 '13 at 08:48
  • That looks like the correct URL. But unless your script is in on the same server hostname, the same-domain policy will block you. The script also probably depends on cookies to hold the login session. – Barmar Mar 31 '13 at 20:39

1 Answers1

0

AJAX does not support requests on other domain. This leaves us with two choices

  1. Fetch response from a server-side code using HttpWebRequest or the likes
  2. Use JSONP. If the other domain supports JSONP callbacks.

Using JSONP is simple and this has been clearly explained in other post on Stackoverflow. You may use method #1 or method #2, both in same question.

If we want to fetch data from other domain which does not support JSONP, then we're left with only option to use server-side code.

Hope this helps!

Vivek

Community
  • 1
  • 1
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
  • thanks. but i also want to know that is there any way that i may know the root address of the url indicated by the code that ive posted? – Kevin Yee Mar 31 '13 at 08:51
  • The root address of the URL would always be relative to the current URL. So, if your URL (in the address-bar) reads http://localhost/home/index, an AJAX call to sched would go to http://localhost/home/sched. If you are not sure of the current URL and would like get the current URL, you may try: `window.location.host` or `$(location).attr('host');` [Reference](http://stackoverflow.com/a/14613849/1654121). – Vivek Jain Mar 31 '13 at 09:15