dousi1970 2016-02-18 18:15
浏览 36
已采纳

使用jQuery .ajax()有效地输出查询结果? [关闭]

Sorry in advance if what I am going to ask is silly, I am having trouble understanding the asynchronous HTTP responses. I am trying to use jQuery .ajax() with PHP and MySQL in the following context:

  • when page loads I will query and return all the applications for a particular id, and then I will use a while loop to output them;
  • also there is a button that allows the user to add new application for that particular id -> on click it:
    • shows a hidden form and gets the values in the inputs;
    • sends the data to PHP using the .ajax() method;
    • performs the insert.
  • and I am stuck at this point

I need to output the newly added application above the existing ones, but the query and while will do it only when an synchronous HTTP request is sent.

I tried to use the .success() but the HTML I need to output inside is really long and I am afraid it will be hard to maintain having outputs from both PHP while loop and jQuery.

Can you please help me understand an efficient way to do this? I have never dealt with asynchronous HTTP request before. Also, I am can't use any JavaScript templating libraries.

I would really appreciate your help!

  • 写回答

1条回答 默认 最新

  • douyan1927 2016-02-18 18:37
    关注

    An ajax postback typically implies some sort of JSON-based web api behind it. Examples of this on the internet abound.

    https://www.lennu.net/jquery-ajax-example-with-json-response/ http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html

    Your REST endpoint should return the minimal amount of JSON data you need. At which point you can use that data to update/bind to elements in your DOM inside your "done" or "success" callback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。