通过ajax发送json对象


                    

我正在创建一个简单的表单输入演示。 值从表单存储在变量中,然后将它们放入json对象,并通过ajax发送。 </ p>

我的Json对象可以验证,但是如何在$ .ajax的数据字段中引用它? 我的代码:</ p>

  $(document).ready(function(){
   $('。submitForm')。on('click',function(event){
event.preventDefault();

     var firstName = $('#firstName')。val();
     var lastName = $('#lastName')。val();
     var phone = $('#phoneNumber')。val();
     var address = $('#address')。val();
     var $ out = $(“#formResults”);

     $ out.append(“

” + firstName +''+ lastName +“ </ p>” +
                 “

” + $('#phoneNumber')。val()+“ </ p>” +

                     // json对象
                      {
                        “ firstName”:“ firstName”,
                         “ lastName”:“ lastName”,
                         “ phoneNumber”:“ phoneNumber”,
                         “ address”:“ address”
                        }

                          $ .ajax({
                          网址:“ http:// localhost / xyz / markup /”,
                          方法:“ GET”,
                          数据:jsonObject
                          错误:警报(“错误”)
                          完成:警报(“完成”)
                                                  });
                                      });
                                      });
</ code> </ pre>

谢谢! </ p>
     </ div>

展开原文

原文

I am creating a simple form input demo. Values are stored from a form in variables then they are put into json object and it is sent via ajax.

My Json object validates but how do I reference it in the data field in $.ajax? My code:

$(document).ready(function() {
  $('.submitForm').on('click',function(event){
event.preventDefault();

    var firstName = $('#firstName').val();
    var lastName  = $('#lastName').val();
    var phone     = $('#phoneNumber').val();
    var address   = $('#address').val();
    var $out      = $("#formResults");

    $out.append("<p>" + firstName  +' '+ lastName + "</p>" +                                       
                "<p>" + $('#phoneNumber').val() + "</p>" + 

                    //json object
                     {
                       "firstName" : "firstName", 
                        "lastName" : "lastName",
                        "phoneNumber" : "phoneNumber",
                        "address" : "address"
                       }


                         $.ajax({
                         url: 'http://localhost/xyz/markup/',
                         method:  'GET',
                         data: jsonObject
                         error: alert("error")
                         complete: alert ("complete")
                                                 });
                                     });            
                                     });

thanks!

1个回答


 “ firstName”:“ firstName”,
</ code> </ pre>

应该是</ p>

 “ firstName”:firstName,
               ^ --- ^-注意缺少引号
</ code> </ pre>

,其他3个字段也是如此。</ p>

您正在尝试执行 string:string </ code>,而不是 string:variable </ code>。</ p>

同样,在代码中没有实际定义 jsonObject </ code>。</ p>

主要注意事项:不要自己构建JSON文本。 风险很大。 一个语法错误(通常是引号错误)和整个json对象都无效。 您最好先构建一个普通的JS数据结构,然后使用提供的json编码工具来生成json字符串。</ p>
     </ div>

展开原文

原文

"firstName" : "firstName",

should be

"firstName" : firstName,
              ^---     ^---note lack of quotes

and similarly for the other 3 fields.

you're trying to do string:string, instead of string:variable.

As well, nowhere in your code is jsonObject actually defined.

Major note: do not build JSON text yourself. It's very risky. One single syntax error (misplaced quote usually) and the whole json object becomes invalid. You'd be better off building a normal JS data structure, then using the provided json encoding facilities to produce the json string.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐