1

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.

Ron
  • 2,435
  • 3
  • 25
  • 34
  • 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? – charlietfl Sep 16 '15 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. – Ron Sep 16 '15 at 16:06
  • Try change `data: {data:100};` to `data: {datas: "100"};` – e-Learner Sep 16 '15 at 16:26
  • Did change that; still get "method not allowed" – Ron Sep 17 '15 at 16:42

1 Answers1

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.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 1
    Actually, there is a pretty simple workaround if you need to call ajax from another domain using a proxy: http://stackoverflow.com/questions/2558977/ajax-cross-domain-call – samrap Sep 16 '15 at 16:08
  • 1
    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 wrestled a bear once. Sep 16 '15 at 16:11
  • 1
    I'm not denying that :) just adding a workaround for op if desired – samrap Sep 16 '15 at 16:14
  • 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 Sep 16 '15 at 16:14
  • @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.. – I wrestled a bear once. Sep 16 '15 at 16:21
  • @samrap - if we're providing alternatives, JSONP is a good one, if your architecture supports it. – I wrestled a bear once. Sep 16 '15 at 16:22
  • never used it, but I believe it only supports GET requests? – samrap Sep 16 '15 at 16:26
  • yeah, no post. It works by taking advantage of the fact that you're allowed to insert scripts from other domains via the script tag. You can't make posts from a script tag, so.. – I wrestled a bear once. Sep 16 '15 at 16:30
  • I copied a file from WW3.org jquery: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_load the file to be loaded is in the same place as the file that failed. Unless Ajax works very differently than load(...), I am confused!!!! – Ron Sep 16 '15 at 16:33
  • @Ron, we're getting closer to figuring out the issue. Use whatever developer tools you have available (firebug, etc) to watch the HTTP requests your browser is trying to make. Make sure it's not calling something from another domain. – I wrestled a bear once. Sep 16 '15 at 16:46
  • I'm not all that familiar with FireBug or Jetbrains. I installed both. Running in Firefox, I don't see any errors at all, but nothing happens. – Ron Sep 16 '15 at 17:19
  • Open firebug and switch to the "Net" tab. Do you see any requests being made when the code is executed? – I wrestled a bear once. Sep 16 '15 at 17:30
  • Firebug says "method not allowed" – Ron Sep 16 '15 at 18:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89847/discussion-between-ron-and-adelphia). – Ron Sep 16 '15 at 18:53