javascript - 又是Safari??表单提交绕过 AJAX

标签 javascript php jquery ajax safari

我的大脑快爆炸了 - 无法弄清楚 Safari 出了什么问题。所有浏览器都可以正常工作,但 Safari 根本无法读取我的 JavaScript。甚至不是一个简单的“alert()”。有什么线索吗?

============HTML==============

</div>  

<div class="container top">

    <h1 class="text">Weather Dashboard</h1>

    <h3 class="text">Enter the city to get a 3-day forecast</h3>


    <form id="cityForm" class="form-group">
        <div class="container-fixed">
            <div class="form-horizontal">

                    <div class="input-group inpWid">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-home"></span>
                    </div>
                        <input type="text" class="form-control" id="city" 
                            name="city" placeholder="Enter the City">
                    </div>

                <button type="submit" class="btn btn-primary btn-success">Weather Me!</button>
            </div>
        </div>
    </form>

<div class="container-fixed" id="weatherBox">

</div>

</div>

============JQuery/JavaScript==============

$("#cityForm").submit(function(e) {
    e.preventDefault();
    alert("safari");
    var url = "scraper.php"; // the script where you handle the form input.
    var city1 = $("#city").val();
    var city = city1.replace(/\s+/g, '');

    $.ajax({
          type: "POST",
          url: url,
          cache:false,
          data: {city, city1},
          success: function(data){
                 $("#weatherBox").html(data); // show response from the php script.
          }
    });

    return false; // avoid to execute the actual submit of the form.
});    

============PHP================

        $city = $_POST["city"];
        $city1 = $_POST["city1"];
        $url="http://www.weather-forecast.com/locations/$city/forecasts/latest";
        $content = file_get_contents($url);

        preg_match('/3 Day Weather Forecast Summary:<\/b>(.*?).<\/span>/s', $content, $day1);
        preg_match('/7 Day Weather Forecast Summary:<\/b>(.*?).<\/span>/s', $content, $day2);
        preg_match('/10 Day Weather Forecast Summary:<\/b>(.*?).<\/span>/s', $content, $day3);

        for ($i=1; $i<=3; $i++) {

            ${d.$i} = '<img src="sun.gif" alt="sun" height="42" width="42">';

            $wCon = ${day.$i}[1];
            preg_match("/dry/i", $wCon, ${weather.$i});             

            if (${weather.$i}[0]!="dry"){
                ${d.$i} = '<img src="rain.png" alt="rain" height="42" width="42">';
            };

            unset(${weather.$i}[0]);
        };

============网站================

http://alexanderii.net/cover/

最佳答案

您有语法错误:- 将 data: {city, city1}, 更改为 data:{'city':city,'city1':city1} 试试这个代码:-

 $.ajax({
      type: "POST",
      url: url,
      cache:false,
      data:{'city':city,'city1':city1},
      success: function(data){
             $("#weatherBox").html(data); // show response from the php script.
      }
});

关于javascript - 又是Safari??表单提交绕过 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664946/

相关文章:

PHP 和 MYSQL 对我的查询有问题!

php - 从单选按钮内部检索查询结果

javascript - 如何使用jquery获取img srcset url

c# - Gridview ASP.NET 锁定第一列和第一行

带有 vars 的 javascript 闭包函数?

javascript - angular 不会显示范围变量(完成)

javascript - React Props 不起作用,完全初学者

php - PayPal IPN 不返回 VERIFIED 为 0

javascript - 如何在div上制作悬停效果

javascript - 使用 php/mysql 构建带有自动播放视频或图像 slider "playlist"的 jquery/javascript HIIT