0

I am trying to brush up on my jquery and ajax. In Jquery in 8 hours there is this:

<!DOCTYPE html>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A JQuery Sample Program</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript"> 
$(function() {
$.ajax({
type:"POST",
url:"postFile.php",
data: {data:100},
success:function(data) {
$("div").html(data);} });}); 
</script>
</head>
<body> 
Response: <div></div>
</body></html>

and postFile.php is this:

<?php
if ($_POST["data"]=="100") {echo "100";}
?>

I'm running this under IISExpress. BUT all I get from the browser (Chrome) is method not allowed in jquery.min.js:4. This seems so simple and yet, doesn't work.

4
  • Strange problem ... use non minified version to isolate more specific part of jQuery that is being used when error thrown. Just to be clear you are opening page on http protocol and not file:// protocol? Commented Sep 16, 2015 at 16:04
  • I changed to jquery.js and now get this error: try { // Do send the request (this may raise an exception) xhr.send( options.hasContent && options.data || null ); } catch ( e ) { (LINE 8625) ALSO, yes, using HTTP protocol. Commented Sep 16, 2015 at 16:06
  • Try change data: {data:100}; to data: {datas: "100"}; Commented Sep 16, 2015 at 16:26
  • Did change that; still get "method not allowed" Commented Sep 17, 2015 at 16:42

1 Answer 1

2

Method not allowed usually happens when you're trying to request a file that's on another domain. I assume that's not your real code since it looks like you're calling a file that's on the same domain. Read about cross domain scripting. You can't do AJAX calls to a script that's on a different domain.

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

14 Comments

Actually, there is a pretty simple workaround if you need to call ajax from another domain using a proxy: stackoverflow.com/questions/2558977/ajax-cross-domain-call
There are workarounds, yes, but they're just that, workarounds. you still cannot make an ajax call to another domain. Making an ajax call to a PHP script that does a curl call is not making an ajax call to another domain. my answer is 100% accurate.
I'm not denying that :) just adding a workaround for op if desired
This is exactly my code. Both files are accessed off the virtual root. They are in the same place, so I'm not sure what the other domain is.
@Ron hmm, well, for whatever reason it looks like your browser thinks it's a different domain. Try and figure out why that might be. If you open it in the browser in a tab next to the HTML, does the URL look the same, etc..
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.