Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我试图从phonegap应用程序执行跨域ajax请求,但它不起作用_Php_Jquery_Ajax_Cross Domain_Phonegap Build - Fatal编程技术网

Php 我试图从phonegap应用程序执行跨域ajax请求,但它不起作用

Php 我试图从phonegap应用程序执行跨域ajax请求,但它不起作用,php,jquery,ajax,cross-domain,phonegap-build,Php,Jquery,Ajax,Cross Domain,Phonegap Build,我试图从phonegap应用程序执行跨域ajax请求,但它不起作用。 我正在尝试将表单数据提交到数据库。 我已经把它上传到了一个我在服务器上使用的测试域中,即使在我的手机浏览器上,它也能正常工作,但是当我用phonegap打包时,它就不能正常工作了。 我已经查看了stackoverflow,但找不到解决方案。谢谢你的帮助 HTML: form_process.php <?php header("Access-Control-Allow-Origin: *"); require("incl

我试图从phonegap应用程序执行跨域ajax请求,但它不起作用。 我正在尝试将表单数据提交到数据库。 我已经把它上传到了一个我在服务器上使用的测试域中,即使在我的手机浏览器上,它也能正常工作,但是当我用phonegap打包时,它就不能正常工作了。 我已经查看了stackoverflow,但找不到解决方案。谢谢你的帮助

HTML:

form_process.php

<?php
header("Access-Control-Allow-Origin: *");

require("include/db_connect.php");
    if(isset($_POST["product_name"])){
        //insert new item into database after submitting add new item form

        $product_name = $_POST['product_name'];

        $stmt_insert_item = $pdoConnection->prepare("INSERT INTO test (testName) VALUES (:testName)");
        $stmt_insert_item->bindValue(':testName', $product_name, PDO::PARAM_STR); 

        $stmt_insert_item->execute();

        $pid = $pdoConnection->lastInsertId();
        $pid_image = "id-" . $pid . ".jpg";
        $image_directory = "images/" . $pid_image;

        $stmt_update_item = $pdoConnection->prepare("UPDATE test SET testImage=:testImage WHERE testID=:pid");
        $stmt_update_item->bindValue(':testImage', $image_directory, PDO::PARAM_STR);
        $stmt_update_item->bindValue(':pid', $pid, PDO::PARAM_INT);
        $stmt_update_item->execute();

        move_uploaded_file($_FILES['file']['tmp_name'], "" . $image_directory);

    }
?>

我在config.xml(对于phonegap)中包含了我认为跨域请求所必需的以下内容:



您的提交事件没有被取消,因此页面被提交,并且在AJAX调用完成之前卸载DOM要添加到上面,请将
e.preventDefault()
放在
submit()
处理程序函数中。您已经将文件两次添加到表单数据对象中,首先使用构造函数,然后使用append.@Rorymcrossan谢谢。我试过了,但它没有屈服。。。所以我把我的按钮改成了id,我用了onclick而不是submit。添加此项…$(“#上载按钮”)。在('click',function(){……})上;但我认为这并没有解决问题。我会回复你的,谢谢你忽略了我的观点:在调用document ready回调之后,你只绑定deviceready侦听器,在此之前,你的侦听器不存在。所有phonegap特定的事件绑定都应该与jQ绑定完全分开。提交事件处理程序应该由jQ绑定。如果需要将两者绑定在一起,请使用
addEventListener
侦听deviceready事件,并使用处理程序绑定jQ document ready回调。你用另一种方式来做,这看起来不对
$(document).ready(function() {
    //detect the deviceready event and call onDeviceReady function
        document.addEventListener("deviceready", onDeviceReady, false);
    onDeviceReady();
});

function onDeviceReady(){
    $("#uploadimage").submit(function(e) {
        var file_data = $('#image_file').prop('files')[0]; 
        var form = $('form')[0];  
            var form_data = new FormData(form);                  
            form_data.append('file', file_data);
        //alert(form_data);                             
            $.ajax({
                    url: 'http://www.testdomain.com/iwp/form_app1/form_process.php', // point to server-side PHP script 
                    dataType: 'text',  // what to expect back from the PHP script, if anything
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         
                    type: 'post',
                    success: function(php_script_response){
                            //alert(php_script_response); // display response from the PHP script, if any
                    },
                error: function(xhr, status, error) {
                alert("sarah" + xhr.responseText);
            }

            });
    });
}
<?php
header("Access-Control-Allow-Origin: *");

require("include/db_connect.php");
    if(isset($_POST["product_name"])){
        //insert new item into database after submitting add new item form

        $product_name = $_POST['product_name'];

        $stmt_insert_item = $pdoConnection->prepare("INSERT INTO test (testName) VALUES (:testName)");
        $stmt_insert_item->bindValue(':testName', $product_name, PDO::PARAM_STR); 

        $stmt_insert_item->execute();

        $pid = $pdoConnection->lastInsertId();
        $pid_image = "id-" . $pid . ".jpg";
        $image_directory = "images/" . $pid_image;

        $stmt_update_item = $pdoConnection->prepare("UPDATE test SET testImage=:testImage WHERE testID=:pid");
        $stmt_update_item->bindValue(':testImage', $image_directory, PDO::PARAM_STR);
        $stmt_update_item->bindValue(':pid', $pid, PDO::PARAM_INT);
        $stmt_update_item->execute();

        move_uploaded_file($_FILES['file']['tmp_name'], "" . $image_directory);

    }
?>
<feature name="http://api.phonegap.com/1.0/network"/>

<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.network-information" />
<access origin="*" />
<plugin name="cordova-plugin-whitelist" version="1" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />