Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a MySQL database full of data that changes frequently. I need to get a string to javascript based on the contents of the MySQL database, and I've concluded that jQuery is the best way to do that. What I'd like to do is something like the following:

var myReturnedString = $.post('myphpcode.php', {myJSData}, function(data) {return data;})

The problem is that even though myphpcode.php echos a string, I think the data passed by jQuery is some kind of object, and I can't figure out how to parse it. Any suggestions?

share|improve this question
    
You first need to tell us if the data is coming back as plain text, JavaScript, JSON, XML, or something else entirely. –  Blazemonger Jul 30 '13 at 13:37
    
That is making an async call. So the object you are getting back is a status object for the request to the server. It's NOT the result of the call. Your function(data) is where the result of the server is, and where you should set ReturnedString –  BlargleMonster Jul 30 '13 at 13:38
    
It's coming back as plain text, I think. All the phpcode does is echo a string based on its parsing of the MySQL. Also, even when I alert the data from function(data), all I get is an object. –  Morslamina Jul 30 '13 at 13:42
    
In general, if you want plain text from PHP you need to set a Content-Type: text/plain header. By default it will specify text/html. It depends on your config though. –  leftclickben Jul 30 '13 at 13:49

2 Answers 2

up vote 0 down vote accepted

When you are calling $.post(), which is really just a wrapper for $.ajax(), you are doing two things: 1, initiating an asynchronous request to the server, and 2, setting up an event handler for when the request is completed (i.e. when the response is received).

This event handler works in much the same way as any other event handler, such as those setup using $.click() or $.keyDown(). So, the $.post() call completes almost instantly and the code after it continues to execute. Then, some time later, the response is received and the callback (function you pass in to $.post()) will be fired.

So what you need is something more like:

$.post('myphpcode.php', {myJSData}, function(data) {
    // this is executed only when the request is complete.
    // the data parameter is the result of the call to the backend.
});
// code here is executed immediately after the request is fired off

P.S. you generally use "post" requests for sending data to the server; if you are only retrieving data, it is more common to use a "get" request, i.e. $.get() instead of $.post().

share|improve this answer
    
This is very helpful, because I'm now realizing that my later code is dependent on the outcome of function(data). Looks like I need to make a non-asynchronous request, which I think I read how to do somewhere. I'll go look it up. Thanks. –  Morslamina Jul 30 '13 at 14:16
    
@user2102695 You almost certainly do not need to use a synchronous ('non-asynchronous') request - if you do, the UI thread will lock up for as long as it takes for the response to come back! You just need to structure your code a bit differently. Maybe you need to abstract some of it out into functions that you can call from inside the callback. –  leftclickben Jul 30 '13 at 14:21
    
I think I probably do. My code is already nested in another asynchronous function. I'm using openlayers and this particular bit of code fetches the url of the tile which openlayers is supposed to display. Because many of the tiles repeat and/or change frequently, I'm keeping the map data in mySQL and grabbing the url of the appropriate tile out of the database. I can't send the correct url for the tile back to openlayers until I've gotten into mySQL and figured out what is supposed to be there. –  Morslamina Jul 30 '13 at 14:30

You must specify the type of returned data.

 $.post('myphpcode.php', {myJSData}, function(data) {return data;},'dataType');

dataType could be text,json or xml

share|improve this answer
    
jQuery will guess the content type based on response headers and/or content, so this generally isn't necessary if you set the right headers on the server side. –  leftclickben Jul 30 '13 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.