2011-03-10 55 views
0

這是我第二天使用jQuery & AJAX。我已經做了儘可能多的googleing,因爲我知道這樣做。但是,不知道我在找什麼,我迷路了。這與工作非常接近,但我無法弄清楚。jQuery/AJAX的API麻煩

我試圖使用我公司的(「xyz」)API,並且當我有表單action =一個到頁面的url時它不會工作。

我已經在PHP中做了這麼多次。 API的網址是:

xyz.com/getdata.php?from=tt&isbn={variable_int} 

有人能幫我一下嗎?

<form method="post" action="xyz.com/getdata.php" id="searchForm"> 
    <input type="text" name="isbn" placeholder="Search..." /> 
    <input class="myaccount" id="doSearch" name="doSearch" type="submit" value="Search" /> 
</form> 

<div id="result"></div> 

{literal} 
<script> 
    // attach a submit handler to the form 
    $("#searchForm").submit(function(event) { 

    // stop form from submitting normally 
    event.preventDefault(); 

    // get some values from elements on the page: 
    var $form = $(this), 
     term = $form.find('input[name="isbn"]').val(), 
     url = $form.attr('action'); 

    // Send the data using post and put the results in a div 
// $.post(url, { doSearch: "Search", isbn: term } , 
    $.post(url, { from: "tt", isbn: term } , 
    function(data) { 
      var content = $(data); 
      $("#result").html(content); 
     } 
    ); 
    }); 
</script> 

非常感謝(提前)!

+0

頁面是否在xyz.com域上運行此腳本?如果沒有,瀏覽器安全規則將阻止您將信息發佈到xyz.com域。 – 2011-03-10 16:22:27

+0

可能是因爲您正在本地主機上運行您的代碼,並且您在公司網址上發佈了您的ajax請求。你在做這個嗎?如果是,那麼你正在提出跨域請求。使用$ .ajax nad dataType = jsonp – 2011-03-10 16:25:18

+0

是的。它運行在不同的域上。 「$ .ajax和dataType = jsonp」將是一種解決方法? – Charlie 2011-03-10 16:28:31

回答

0

我不知道,但如果你使用下面

xyz.com/getdata.php?from=tt字符串& ISBN = { variable_int}

發送數據的方法是「get」。表單使用「post」方法。我認爲有衝突。

+0

我試着改變它們以匹配使用get&post。 – Charlie 2011-03-10 16:32:10