1

so I am trying to access some content in an external php file (not on my domain) however I can browser to the content but I cannot access it via jquery ajax here is my code:

function newarticle()
{
var ajax_load = "<img src='images/spiral.gif' align='center' alt='loading...' />";  

//load() functions  
var loadUrl = "http://www.webapp-testing.com/includes/newarticles.php";  
$("#articles").click(function(){  
    $("#pagetitle").html(ajax_load).load("New Articles");
    $("#listarticles").html(ajax_load).load(loadUrl);  
}); 
}

what am i doing incorrect to access this content?

3 Answers 3

2

You can only make AJAX requests on the same domain due to the Same Origin Policy. Your best bet is to make a server-side (in something like PHP) proxy. You would then make the request on the proxy (which would be on the same server), which would make a request to the page and return the information.

Sign up to request clarification or add additional context in comments.

1 Comment

Like I told you. Make a server-side script to do the request for you and put it on the same domain as your JavaScript.
1

As Xeon06 mentioned - you can't make cross site requests with AJAX. You can have a look at JSONP though that basically allows you to do it.
Link to jQuery docs
Link to JSONP Wikipedia

2 Comments

...if you can modify the page you are attempting to read from to support JSONP.
I can modify it I just dont know how to do such
0

Create your own php file that get the content of the desired url with a curl. -> http://ch.php.net/manual/de/book.curl.php

=> your_php_file.php

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.webapp-testing.com/includes/newarticles.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($curl);
curl_close($curl);

print $page;

?>

In your javascript code, call your_php_file.php

function newarticle()
{
var ajax_load = "<img src='images/spiral.gif' align='center' alt='loading...' />";  

//load() functions  
var loadUrl = "your_php_file.php";  
$("#articles").click(function(){  
    $("#pagetitle").html(ajax_load).load("New Articles");
    $("#listarticles").html(ajax_load).load(loadUrl);  
}); 
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.