JQuery: Ajax call returning an undescriptive error message
Problem Description
I receive an non-descriptive "error" message whenever attempting to call an HTML page. However, there are response headers returned, but no actual response. I've gone as far as stepping through the JQuery and now at a loss of what else I can do?
Ultimately, I am attempting to get the following URL http://www.impre.com/uForms/external/getWeatherByZip?modules=top to display in a div I've created in the HTML of the main page.
Code
<script type="text/javascript">
$.ajax ({
type: "GET",
url: "http://www.impre.com/uForms/external/getWeatherByZip",
data: "modules=top",
success: function(data)
{
alert('Load was performed.');
$('#accuWeatherContainerTop').html(data);
},
error: function(data, responseText)
{
try
{
console.log('errData: ', data);
console.log('responseText: ', responseText);
} catch(e) {
alert("You don't have Firebug!\nFor shame...");
}
},
complete: function(jqXHR, textStatus) {
console.log('com_jqXHR', jqXHR.getAllResponseHeaders());
}
});
</script>
Notes
Upon several iterations, I worked out several kinks such as a 301 error message which I was getting. I finally got the 开发者_运维百科GET response code of "200 OK" but still no response text.
The url I am attempting to connect to is using the Zend Framework which I'm unsure how it would ultimately affect the AJAX request.
RESOLUTION
It was indeed a Same-Origin policy violation and all of the responses were accurate in this suspicion. The original problem was the 301 error which I had resolved; on a whim I went ahead and pushed it onto the live website and the production server now properly displays the widget. Thanks!
It's probably problem of Same Origin Policy
as you're loading it from not-your-own domain
A better way to debug this ajax call would be to add a third parameter to your error function. From the jquery documentation:
error(jqXHR, textStatus, errorThrown)Function
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
The third parameter, errorThrown, may have some meaningful information in it that will help you move forward with your fix.
What you should do is make an AJAX request to a file on your server, let's say request.php
. In request.php
, you're going to do some sort of request (depending on which server side language you're using, if it is indeed PHP -- cURL
,) to request the content from the page you were initially trying to get via AJAX alone. Then, print that response.
As @genesis said, what you are trying to do is XSS, and it is forbidden.
精彩评论