I'm calling a PHP file, and it's returning the response when I view it in Firebug. However, it's not displaying on my page. I'm not sure if the success is actually firing, because I can't even run an alert, but it is working because the PHP code is firing and returning what it's supposed to.
<script type="text/javascript" src="//code.jquery/jquery-1.10.2.min.js"></script>
<script>
$.ajax({
type: "GET",
url: '',
dataType: 'jsonp',
contentType: "text/html",
crossDomain:'true',
success: function (data) {
$(".result").html(data);
}
});
</script>
<div class="result"></div>
I'm calling a PHP file, and it's returning the response when I view it in Firebug. However, it's not displaying on my page. I'm not sure if the success is actually firing, because I can't even run an alert, but it is working because the PHP code is firing and returning what it's supposed to.
<script type="text/javascript" src="//code.jquery./jquery-1.10.2.min.js"></script>
<script>
$.ajax({
type: "GET",
url: 'https://www.example.',
dataType: 'jsonp',
contentType: "text/html",
crossDomain:'true',
success: function (data) {
$(".result").html(data);
}
});
</script>
<div class="result"></div>
Share
Improve this question
edited May 18, 2017 at 16:40
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Jan 2, 2014 at 19:13
user2570937user2570937
8523 gold badges18 silver badges35 bronze badges
1
- You seem to be sticking params on your ajax request that don't belong. For example, why are you using contentType: "text/html" if you aren't sending text/html to the server? Why are you using .html() to insert possibly a javascript object or javascript array to the content of a div? My guess is you are making a cross-domain request to retrieve text/html which isn't allowed due to the same origin policy, so you started adding in parameters to try to force it to work. – Kevin B Commented Jan 2, 2014 at 19:20
2 Answers
Reset to default 4You need to make the ajax call on load or document ready. The div may not exist at the time of response (page is interpreted top down).
Just a little lesson about what your are doing.
When working with any form of Ajax (or JavaScript in general), it is usually imperative to have your console open. You will be able to see your request, your response, and whole other useful information.
I usually work with Firefox and Chrome from time to time.
You can open your console by right clicking on the page anywhere and inspect an element with Firebug (on Firefox).
And inspect element in Chrome.
Once you see the Ajax call, click the + and see its content.
You should see something like:
Headers Post Response JSON Cookies or something alike.
I hope that helps.