獲取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);
}
});
}
刪除' 「」「''左右JSON.stringify()'。單引號在JSON字符串中無效。 - 儘管如此,沒有必要在之後立即將stringify()僅用於parse()。你可以傳遞'Object'本身作爲參數。 –
你似乎認爲只有字符串可以作爲參數傳遞給函數。通過'data'和你的回調函數,不需要將它們轉換爲字符串並在被調用者中解析。 –
此外,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()'。 –