javascript
  • jquery
  • json
  • 2016-05-30 37 views 0 likes 
    0

    获取JSON错误,同时呼吁获取JSON错误,同时调用多个功能

    Error - Uncaught SyntaxError: Unexpected token ' in JSON at position 0

    我打电话jQuery的多种功能,但它给错误我已经尝试了这么多的方式,但错误得到改变,但它不是多个功能工作我该怎么做。

    HTML

    <div id="div1"> 
        <input type="submit"onclick='Function1()'> 
        <input type="text" value="Text1" id="input1"> 
        <input type="text" value="Text2" id="input2"> 
    </div> 
    
    <div id='div2'></div> 
    

    jQuery的

    function Function1(){ 
        var input1 = $("#input1").val(); 
        var input2 = $("#input2").val(); 
        var datajson = { "input1" : input1, "input2" : input2 }; 
        var data = "'"+JSON.stringify(datajson)+"'"; 
        Post_Funtion('testpost.php',data,'$("#div2").html(html);') 
    } 
    
    function Post_Funtion(URl,DATA,FUNCTION){ 
    
        var url = encodeURIComponent(URl); 
        var data = JSON.parse(DATA); 
    
        $.ajax({ 
         type: "POST", 
         url: url, 
         data: data, 
         cache: false, 
         success: function(html) { 
          eval(FUNCTION); 
         } 
        }); 
    } 
    
    +2

    删除' “”“''左右JSON.stringify()'。单引号在JSON字符串中无效。 - 尽管如此,没有必要在之后立即将stringify()仅用于parse()。你可以传递'Object'本身作为参数。 –

    +0

    你似乎认为只有字符串可以作为参数传递给函数。通过'data'和你的回调函数,不需要将它们转换为字符串并在被调用者中解析。 –

    +0

    此外,JavaScript中的函数可以像任何其他值一样使用和传递。因此,'FUNCTION'可以是一个[实际的'函数'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function) - 'Post_Function(...,function (html){$(“#div2”)。html(html);})' - 稍后用'FUNCTION(html);'或甚至'success:FUNCTION'调用,而不需要'eval()'。 –

    回答

    0

    您试图JSON.stringify()后的数据有误,然后转身和解析回传给AJAX选项。

    这整个步骤是不必要的。通过原样传递对象和jQuery会照顾它从那里

    function Function1(){ 
        var input1 = $("#input1").val(); 
        var input2 = $("#input2").val(); 
        var data = { "input1" : input1, "input2" : input2 }; 
        Post_Funtion('testpost.php',data,'$("#div2").html(html);') 
    } 
    
    
    function Post_Funtion(URl,DATA,FUNCTION){  
        var url = encodeURIComponent(URl);  
    
        $.ajax({ 
         data: DATA, 
         /* other options the same */ 
    
        .... 
    } 
    
    0

    您正在试图调用JSON.parse上的绳子,看起来像这样:这是没有很好地形成JSON

    '{"input1": input1, "input2": input2}' 
    

    应该删除外部引号。试试这个代码:

    function Function1(){ 
        var input1 = $("#input1").val(); 
        var input2 = $("#input2").val(); 
        var datajson = { "input1" : input1, "input2" : input2 }; 
        var data = JSON.stringify(datajson) // Removed quotes from JSON 
        Post_Funtion('testpost.php',data,'$("#div2").html(html);') 
    } 
    
    function Post_Funtion(URl,DATA,FUNCTION){ 
    
        var url = encodeURIComponent(URl); 
        var data = JSON.parse(DATA); 
    
        $.ajax({ 
         type: "POST", 
         url: url, 
         data: data, 
         cache: false, 
         success: function(html) { 
          eval(FUNCTION); 
         } 
        }); 
    } 
    
    0

    “ ' ”+ JSON.stringify(datajson)+“'”; - 删除引号

    var data = JSON.stringify(datajson); 
    

    在这种情况下无法看到字符串化和解析对象的原因。请尝试以下操作:

    function Function1(){ 
    var input1 = $("#input1").val(); 
    var input2 = $("#input2").val(); 
    var data = { "input1" : input1, "input2" : input2 }; 
    
    function cb(response){ 
        $("#div2").html(response); 
    } 
    
    Post_Funtion('testpost.php',data, cb) 
    } 
    
    function Post_Funtion(URl,DATA,FUNCTION){ 
    
    var url = encodeURIComponent(URl); 
    
    console.log(DATA); 
    
    $.ajax({ 
        type: "POST", 
        url: url, 
        data: DATA, 
        cache: false, 
        success: FUNCTION 
    }); 
    } 
    

    PS不使用eval,Why is using the JavaScript eval function a bad idea?

    +0

    我正在阅读有关eval()我有很多点不使用此功能,但我有一个问题,什么函数可以在eval()的位置使用? – User97798

    +0

    我编辑了我的答案,看看函数cb(response){..} –

    相关问题