从与jQuery Ajax响应显示的数据数据、jQuery、Ajax

2023-09-10 19:48:56 作者:宣久泽子

我想学习如何使用jQuery的Ajax响应

I am trying to learn how to display data from an Ajax response with jquery

这是是,则返回到控制台

This was is what is returned to the console

Object {type: "success", message: "Your message has been sent, thank you.", record: Object}
record: Object
account_number: "1234567812345678"
balance: "1234"
bank_name: "test name"
customer_id: "12345"
id: 49
monthly: "123456"

这是当前的脚本,把它到控制台:

This is the current script that brings it to the console:

jQuery(document).ready(function($) {
  $('form.quform').Quform({
    successStart: function (response) {
      console.log(response); 

这是我所尝试,但它不能正常工作,从当你在底部添加额外的右括号工作停止AJAX,但不起任何作用,如果你把它关闭:

This is what I have tried but it does not work, it stops the AJAX from working when you add the extra closing bracket at the bottom, but it does nothing if you take it off:

jQuery(document).ready(function($) {
    $('form.quform').Quform({
        successStart: function (response) {
            var r = response.record;
            var html = '<li>Acct#: ' + r.account_number + '</li><li>Balance: ' + r.balance + '</li><li>Bank: ' + r.bank_name + '</li><li>Customer#: ' + r.customer_id + '</li>';
            $("#ulID").html(html);
        }
    });
});

请注意我已经加入&LT; D​​IV ID =ulID&GT;&LT; / DIV&GT; 进入该页面,因为我觉得这就是我需要显示它

Please note I have added <div id="ulID"></div> into the page because I think that is what I need to display it

推荐答案

我必须承认,我不熟悉使用AJAX的方式来获得一个回应 你查询一个web服务? 而我不熟悉的Quform的插件使用,应该指定你的问题,你正在使用的插件。

I must admit that i am unfamiliar with the way you use ajax to get a 'response' Are you querying a webservice? And i am not familiar with the Quform plugin either, you should specify that in your question that you are using a plugin.

不过,我会尽力帮助你反正。 因为你的问题问从Ajax响应显示数据,我会假设你要查询一个Web服务和尝试,并帮你出这样的假设 这里是code调用Ajax的web服务,而无需使用插件。

But i will try and help you anyways. Since your question asked for displaying data from an Ajax response, i will assume that you are querying a webservice and try and help you out with that assumption Here is the code to call a webservice with Ajax without using a plugin.

$.ajax({
    url: 'webservice url',
    type: "POST",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
            alert('Success!');
        $('.ulID').text('response was: ' + data.d)
    },
    failure: function (msg) { 
                alert('an error occured'); 
        }
});

网址是offcourse链接到Web服务,数据包含了Web服务需要的任何参数。

The url is offcourse the link to the webservice, and the data contains any parameters that the webservice require.

这是我的知识使用Ajax查询Web服务的正确方法。

This is to my knowledge the correct way to use Ajax to query a webservice.

如果你想与帮助您使用的code你已经有了我建议从问题中移除阿贾克斯和Quform取代它帮助。 不过说实话,阿贾克斯JQuery的要求是那么容易,除非你打算使用该插件别的东西,我就把你的插件。

if you want help with getting help with the code you already have i suggest removing Ajax from the question and replacing it with Quform. But to be honest, Ajax calls in JQuery are so easy that unless you plan on using that plugin for something else i'd drop the plugin.

BTW:这个设置期望Web服务支持JSON

btw: this setup expects the webservice to support json

我希望这有助于