jQuery Ajax POST unsuccessful

后端 未结 3 1019
悲哀的现实
悲哀的现实 2020-12-11 18:37

I\'m unable to successfully post using jquery\'s ajax functionality.

URL of the running page is http://localhost:9999, URL of the target (web service)

相关标签:
3条回答
  • 2020-12-11 19:26

    This is an issue with cross-domain ajax calls. Basically (at least in Firefox), a POST request is converted to an OPTIONS request for security reasons. I ran into the same exact thing last night, posted here.

    WCF Ajax Call not working with Jquery $.ajax

    I had an $.ajax call I was making on localhost:23485, to a web service on http://localhost hosted in IIS. Because they are different domains, cross-domain kicked in and made things difficult.

    0 讨论(0)
  • 2020-12-11 19:29

    What handler gets called? The success or error handler? Can you elaborate what "doesn't work" means?

    You probably want alert more information in your error handler, like so:

    error: function(XMLHttpRequest, textStatus, errorThrown) {
       //console.log is better at least for debugging. You can change this back to alert 
       //when your code goes into production
       console.log("Update unsuccessful. Status: ", textStatus, " error thrown: ", errorThrown);
    }
    

    textStatus should give you an idea as to what the problem could be. Possible values are "timeout", "error", "notmodified" and "parsererror". Once you figure out the actual error, please update the question.

    Also if you have Firebug check the Net tab to see the request and the response. A few common sources of errors:

    • Violating same-origin policy. You cannot make an AJAX to a different URL from the parent page.
    • Server-side errors. If your server returns something like a 500 response code, the request will fail.
    • Parsing errors. If you're expecting a specific response format like json or xml, and if the response is not in that format, the AJAX request will be unsuccessful.
    0 讨论(0)
  • 2020-12-11 19:39

    Make a proxy on the same domain you are calling the ajax, e.g. in PHP:

    <?php /* get.php */
        $url = $_GET["Url"];
        echo file_get_contents($url);
    ?>
    

    Make your ajax call:

    $.ajax({ url: "get.php?Url=realurl.com" });
    

    That's one workaround.

    0 讨论(0)
提交回复
热议问题