ps0604
ps0604

Reputation: 1071

jQuery ajax call only from HTML page

Given the following jQuery code that makes an ajax call:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

The problem I have is that the user could manually enter test.html on the URL and see the response on the screen. How to avoid that?

Upvotes: 4

Views: 1068

Answers (3)

Ajay Chaudhary
Ajay Chaudhary

Reputation: 308

One way you an do this to create a variable for in some other script or location so the user will only see the variable name.

Also send you request through post method and make you server ajax reponse page respond to POST request only( say using some server side language like PHP, ASP.Net or JSP). This way even if the user find out the path of the url it will not be able to view its contents.

Upvotes: 3

Ryan Boyd
Ryan Boyd

Reputation: 193

Try adding some kind of authorization in the HTTP header. Use the Ajax beforeSend setting. Then authenticate the header with PHP or any other server side language.

Upvotes: 3

S.Galarneau
S.Galarneau

Reputation: 2234

No you can't, AJAX calls are basically just HTTP calls and can be access with direct browsing.

Upvotes: 0

Related Questions