display message on success return in ajax with jquery problem - ajax

i am using ajax with jquery .i have written this code:
$.ajax({url:"myurl.php",
data:datastr,
type:"POST",
success:function(data){
$("#msgbx").text(data);
}
});
it done what i want at the backend but doesn't show the return message in div with #msgbx id.
what is the problem?

The problem may be related to the formatting or encoding of your returned JSON object, or it may be a failure on the server that occurs after all your other processing is done, but immediately before you return your data.
Without seeing any other code, try looking at the console window in either Firebug or Chrome's web development tools to see what your server is responding with. You should see an Ajax POST method logged to the console window. Assuming you don't have an error code shown there, check to see what sort of data is being returned (or if any is being returned at all).
If you are getting data back from the server, your problem is in how you're displaying that on the page / inserting it into the DOM. If on the other hand you aren't returning anything, you'll need to check your PHP code to see where other problems might exist in your logic.
Feel free to add more to your post so we can give more specific answers.

use $("#msgbx").html(data);

Related

How to debug a failed ajax request in google chrome?

I have a web application that crashes on ajax requests with google chrome (it works with every other web browser it was tested it). After debugging I found that the error is caused by response.responseText being undefined. The xhr object looks like this:
argument: undefined
isAbort: false
isTimeout: undefined
status: 0
statusText: "communication failure"
tId: 3
In debugger in the 'network' tab I get "(failed)", however all the headers are there and I can even copy into clipboard the response body (which is a valid JSON).
My question is - how can I debug this problem? Where to find additional information, what causes this request to fail?
I finally found the solution to my problem : AdBlocks, when it blocks an ajax request, it just says "communication failure".
The first thing I would double-check is that the data coming back from the response is valid JSON. Just pass it through a JSON validator like this online JSONLint: http://jsonlint.com/
I assume that you are using something like jQuery to make your AJAX requests. If so, then make sure that you are using the development version of that library. Now that you are using the development version (uncompressed) of the script, find the particular function that you are using (eg. $.ajax) and then, within the Chrome inspector, insert a breakpoint in the code where the AJAX response is first handled (eg. https://github.com/jquery/jquery/blob/master/src/ajax.js#L579). Then proceed to step through the code, inspecting various return-values to see exactly what is going wrong.
If you are not using something like jQuery to make AJAX calls, then I'd recommend using a framework to avoid possible cross-browser compatibility issues like you might be experiencing right now.

getting JSON from PhoneGap application ?

i'm trying to send a getJson call from my app to get an external JSON from PHP file. all i'm getting is a gray empty screen ? can anyone tell me what's the problem or at least how can i debug to get the problem
here also attached the PHP file running !!
You need to provide more Information . Try to debug using Firebug , This is available for both chrome and firefox and since your using html run it in your browser and see what you get.
Do console.log(data); and see what is being outputted in your firebugs console tab. You might be running into some cross domain issue too. Use jquery ajax jquery JOSNP to over come this. just put data: jsonp .
Hope this helps you , please provide more information.

Django: How to track down a spurious HTTP request?

I have 3 AJAX functions to move data between a Django app on my website and some JavaScript using YUI in the browser. There is not a major difference between them in terms of their structure, concept, code, etc. 2 of them work fine, but in the 3rd one, I get one spurious HTTP request immediately after the intended request. Its POST data contains a subset of the POST data of the intended request. The request meta data is identical except for the CONTENT_LENGTH (obviously) and the CONTENT_TYPE which is 'text/plain; charset=UTF-8' for the intended and 'application/x-www-form-urlencoded' for the unwanted request. I do not set the content type explicitely at all which seems to suggest both requests do not have the same origin and the second one just pops out of thin air.
The intended request sets HTTP_CACHE_CONTROL': 'no-cache' and 'HTTP_PRAGMA': 'no-cache', the spurious one does not. The dev server log output for both requests is
[15/Feb/2010 15:00:12] "POST /settings/ HTTP/1.1" 200 0
What does the last 0 at the end mean ? Could not find any documentation on that. This value is usually non-zero... In Apache, it is the total size in bytes of the server response, can someone confirm it's the same for Django ?
My problem obviously is to find out where this additional request comes from.
I am fairly familiar with JS debugging using Firebug and I think I'm good at Python and Django, but I do not know a lot about the internals of HTTP requests and responses. I can breakpoint and step through the JS code that sends the intended XMLHTTP request, but that breakpoint does not get hit again.
The problem occurs with both FF3 and Safari, I'm on Snow Leopard, so I can't test with IE or Chrome.
I've looked at Django debugging techniques and tools like http://robhudson.github.com/django-debug-toolbar/ but I think I already have the information they can give me.
Can someone advise on a strategy or a tool to narrow the problem down ?
The problematic AJAX function submits form data, the working two don't. Forms have a default action which takes place when the form is submitted: post a request with the form data. I failed to prevent this default action.
So the spurious request did indeed come out of the dark underwood of the browser, there is no code in my js files that sends it.
Solution:
YAHOO.util.Event.preventDefault(event);
at the beginning of the form submit event handler.
See also http://developer.yahoo.com/yui/examples/event/eventsimple.html

Should ASP.NET AJAX be avoided in heavy pages?

I'm getting some js errors only for some users, and only every once in a while on a page that uses quite a bit of ASP.NET AJAX.
The page also does some intense SQL querying and some string manipulation to highlight text found in the search results.
Could this be a result of performance? Is it always safe to use ASP.NET AJAX in demanding situations or should I be looking to other AJAX techniques?
(By the way the errors I sometime see are):
Message: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 12031
Message: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near ' '.
ASP.NET AJAX has been known to not be the most performance intensive approach, but that's what you got i suppose in exchange for how simple it is to implement.
I do know you aren't allowed to do any Response.Writes within an update panel. That will cause your second error.
This particular exception is very
common and can be caused by any one of
the following:
1. Calls to Response.Write():
By calling Response.Write() directly you are bypassing the normal
rendering mechanism of ASP.NET
controls. The bits you write are going
straight out to the client without
further processing (well, mostly...).
This means that UpdatePanel can't
encode the data in its special format.
2. Response filters:
Similar to Response.Write(), response filters can change the
rendering in such a way that the
UpdatePanel won't know.
3. HttpModules:
Again, the same deal as Response.Write() and response filters.
4. Server trace is enabled:
If I were going to implement trace again, I'd do it differently.
Trace is effectively written out using
Response.Write(), and as such messes
up the special format that we use for
UpdatePanel.
5. Calls to Server.Transfer():
Unfortunately, there's no way to detect that Server.Transfer() was
called. This means that UpdatePanel
can't do anything intelligent when
someone calls Server.Transfer(). The
response sent back to the client is
the HTML markup from the page to which
you transferred. Since its HTML and
not the special format, it can't be
parsed, and you get the error.
Complete Post : ASP.NET AJAX and Sys.Webforms.PageRequestManagerServerErrorException
You can grab the code which causes the error by using Visual Studio Debug feature. I don't know much but maybe it can help and also Firebug will help you to see server response and data you submit to the server.
Here is a video where you can see how to use Firebug to debug Ajax.
See how I used Firebug to learn jQuery
But I don't think Asp.NET Ajax should be avoided in heavy loaded pages. That is actually what Ajax stands for right ? I mean it also relieves servers to send small pieces of pages instead of requesting the whole page again.

NS_BINDING_ABORTED Shown in Firefox with HttpFox

I am seeing some of the server calls (Used for tracking purpose) in my site getting aborted in Firefox while seeing through HttpFox. This is happening while clicking some link that loads another page in the same window. It works fine with popup. The error type shown is NS_BINDING_ABORTED. I need to know is the tracking call is hitting the server or not.
It works perfectly with Internet Explorer. Is it any problem with the tool? In that case can you suggest any that can be used in Firefox too.
Because your server is not sending HTTP Expires headers, the browser is checking to see if what is in its cache is current.
The way it does this is to send the server a request saying what the date of what it has in the cache is, and the server is sending 304 status telling the client that what it has is current. In other words, the server is not sending the entire content again but instead sending just a short header to say the existing cache content is current.
What you probably need to fix, is to add Expires headers to what you are serving. Then you will see the NS_BINDING_ABORTED message change to (cache), meaning the browser is simply getting content out of its cache, knowing it has not yet expired.
I should add that, when you do a FireFox forced refresh, it assumes that you want to double-check what is in the cache, so it temporarily ignores Expires.
You shouldn't be worried just because you see something that looks like a failure code (NS_BINDING_ABORTED).
In one post a Firefox developer confirms that NS_BINDING_ABORTED is simply an indication that a page load has been stopped.
It seems perfectly normal that opening a page while another page is being loaded cancels the loads on the first page. It doesn't necessarily mean the loads were aborted before the request got sent to the server, which seems to be what you care about.
[edit: reworded & removed the bit about me not being familiar with HttpFox, as people who see this in 2022 are probably not using it anyway.]
What other javascript do you have on the page? Some javascript might be firing causing the request to be aborted.
I noticed the same thing in my application. I was redirecting the page in javascript (window.location = '/some/page.html') but then further down the block of code, I was calling 'window.reload()'. The previous redirection was aborted because window.reload was called.
I don't know what tracking you are using but it's possible that the request is being sent to your server but the request is aborted because another request was issued afterwards.
I have experienced a similar problem, but have identified the cause.
I have a link in the first cell of a table row, and some Javascript that replicates that link across the other TD's of the row. When I click on the 'real' link (in the first cell) I get this unwanted side-effect; when I click on other cells in the row, all is fine. I feel it's because the script is adding a second link to that first cell, when it already has one.
Hence, two instantaneous requests for the same page, with the first being aborted by the second.
This technique is fairly common, so something to look out for.
NS_BINDING_ABORTED error - Best Approach -Using a JavaScript “setInterval” method with the time delay of Min ‘0’ to max ‘100’ milliseconds based on the page load, we can execute our track link request after the default page submit request is processed.
World best solution:
var el = document.getElementById("t");
el.addEventListener("click", avoidNSError, false); //Firefox
function avoidNSError(){
ElementInterval = setInterval(function () {
/* Tracking or other request code goes here */
clearInterval(ElementInterval);
},0);
};
In my case, same NS_BINDING_ABORTED error, but it was because a "button" element, which I clicked to trigger an event, was missing the attribute "type" value "submit" = How to prevent buttons from submitting forms
The error NS_BINDING_ABORTED can have a variety of reasons.
In my case it was garbage in the response headers received from the server, basically a HTTP protocol violation.
Using a web debugging proxy such as Fiddler may sometimes reveal such issues better than the browser's own debugging console (which today does what, I assume, HttpFox did, just better), or at least show more detailed information or clearer error messages.
I know this is a very old question but this happened to me recently with Firefox 95. The images of an ancient application made by a collegue of mine were not loaded (or loaded randomly) because of this code:
window.addEventListener('focus', function() {
// omit other code...
location.reload();
}
Once nested this code into a 'load' listener, the issue completely disappeared.
in my case experience, NS_BINDING_ABORTED occurred because missing closed tag between <form>...</form>
example:
<form name="myform" action="submit.php" method="post">
<div class="myclassinput">
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="submit" value="Submit">
</form>
there is I am forget to write closing </div> tag before </form>.
I note my experience here, just in case... For me it was a website on a local dev server (adress 192.... etc) which was put online on an already used URL like www.something.com
The consequence was that an MP4 video (through the H5P library) didn't play, but allowed to be scrolled through the progress bar. And when I copy/paste the URL to this video, this NS_BINDING_ABORTED error appeared on my laptop, while my colleague on the same internet connection had no problem to view it.
I made an ipconfig /release and /renew, then restarted my computer, and it was fixed... maybe it was some old data conflict with the previous content on this already used URL domain? I don't know.
For me reason was in Firefox browser preventDefault function not worked in form submit event. This answer helped to solve: https://stackoverflow.com/a/56695472/2097494

Resources