douyun3887 2017-07-24 22:18
浏览 101
已采纳

页面不会从iOS重定向到URLRequest

I'm trying to make a URLRequest from iOS with Swift and I send some data by the url to a kind of PHP server.

let url = URL(string: "http://www.mybadservice.com/redirectToAction.php?id=\(id)")


    print(stringUrl)

    var mutableRequest = URLRequest(url: url!)

    URLSession.shared.dataTask(with: mutableRequest) { (data, urlResponse, error) in
        if error != nil {
            print("Error running this ...")
            return
        }

        let statusCode = urlResponse as? HTTPURLResponse
        print("HTTP RESPONSE : \(statusCode?.statusCode)")
        let dataString = String(data: data!, encoding: .utf8)
        print("DATA : \(dataString)")
    }.resume()

When I send the data to redirectToAction.php it should gets the data using GET, makes a url and redirect to another page using javascript and this page does some work.

<html>
<head>  
    <title></title>
</head>
<body>


    <script type="text/javascript">    
        location.href ="./DoSomeWork.php?id=passedId"; /*Where pasedId is the paramether I send by url, I don't now how exactly passedId is linked together from php to javascript because I have not access to the code */
    </script>
</body>

Now it does not works just when I make a urlResponse. I tried to copy the url generated by the app and paste it in the browser and it works perfectly. Also the request in the app returns a 200 http code and prints the whole html page.

  • 写回答

1条回答 默认 最新

  • drsh30452 2017-07-25 16:45
    关注

    This will not work, because php is server side and the server can not redirect to a page in a unrelated browser.

    Trust me, in one year you will cry tears if you read your question again :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试