drake900918
2013-07-30 13:35
浏览 51
已采纳

使用jQuery将PHP回显的字符串分配给Javascript中的变量

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?

图片转代码服务由CSDN问答提供 功能建议

我的MySQL数据库中充满了经常变化的数据。 我需要根据MySQL数据库的内容获取一个字符串到javascript,我得出结论,jQuery是最好的方法。 我想做的事情如下:

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

问题是即使myphpcode.php回显一个字符串,我认为jQuery传递的数据是某种对象,我可以 弄清楚如何解析它。 有什么建议吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douqiao6015 2013-07-30 14:04
    最佳回答

    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().

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题