weixin_33725807 2016-10-18 08:41 采纳率: 0%
浏览 26

通过AJAX发送JSON数据

I'm trying to send data from option selector

<select id="city">
    <option value="{'city':'melbourne','lat':'-37.8602828','long':'144.9631'}">Melbourne</option>

Through an AJAX POST

$.ajax({
    method: "POST",
    url: "action.php",
    dataType: "json",
    data: {
        city: $('#city option:selected').val()
    },
})

However I got an empty $_POST in action.php. What is the correct way to send the value of the selected option through an AJAX request?

  • 写回答

2条回答 默认 最新

  • csdnceshi62 2016-10-18 08:50
    关注

    You have a missing =" at the option value attribute

    <select id="city">
        <option value="{'city':'melbourne','lat':'-37.8602828','long':'144.9631'}">Melbourne</option>
    

    Plus, you can directly access the select value:

    $.ajax({
        method: "POST",
        url: "action.php",
        dataType: "json",
        data: {
            city: $('#city').val()
        },
    })
    

    At the level of PHP, you have to decode the value of $_POST['city'] to get a mapped PHP array:

    $city = json_decode($_POST['city']);
    
    评论

报告相同问题?