我正在尝试从phonegap应用程序执行跨域ajax请求,但它无法正常工作

时间:2016-07-04 13:14:35

标签: php jquery ajax cross-domain phonegap-build

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

HTML:

<form id="uploadimage" method="post" action="" enctype="multipart/form-data" >
    <label>name</label>
    <input type="text" name="product_name" id="product_name" size="50"/>                            
    <label>Image</label>
    <input type="file" id="image_file" name="image_file"/>
    <input type="submit" class="button_style" value="Add this item">
</form>

JQuery的:

$(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);
            }

            });
    });
}

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):

<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://*/*" />

0 个答案:

没有答案