开发者

Ajax Sending First Blank Data

This part of my code is where I think the problem is at:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">

matches = google.contentmatch.getContentMatches();
    $(document).ready(function()
    {
        var messageid
        for(var match in matches)
        {
            for(var key in matches[match])
            {
                if(key == 'message_id')
                {
                    messageid = matches[match][key];
                }

            }
        }




        var name = "Email Test";
        var url = 'https://mail.google.com/mail/u/0/#all/' + messageid;
        var encodedurl = encodeURIComponent(url);
        开发者_如何学Pythonvar dataValues = "name=" + name + "&url=" + encodedurl;
        $('#crmbtn').click(function(){
            $.ajax({
                type: 'GET',
                data: dataValues,
                url: 'http://someurl',
                success: function(){
                     alert('Sent To CRM')
                }
            });
            return false;
        });
    });
gadgets.window.adjustHeight(50);
</script>

    <form id="myForm">
        <input type='button'  name='submit' id='crmbtn' value='Send To CRM' />
    </form>

The issue is that my code does what I want it to do, but instead of sending my data once it first sends blank data to my other php page and then sends the correct data afterwards. Im guessing its the way I coded my ajax function.


You need to encodeURIComponent the name parameter as well:

var name = encodeURIComponent("Email Test");
…
var dataValues = "name=" + name + "&url=" + encodedurl;

Moreover, it's better to bind that event to your form's submit handler and prevent that action, like:

<form id="myForm">
    <input type='submit' name='submit' id='crmbtn' value='Send To CRM' />
</form>

(changed type to submit) and

$('#myForm').submit(function(){ …

And when you're iterating over objects, always check hasOwnProperty:

for(var match in matches)
{
    if (matches.hasOwnProperty(match)) {
        …


This is just a wild guess (I need to see more of the code...), but if #crmbtn is a link, you need to change:

$('#crmbtn').click(function(){

to:

$('#crmbtn').click(function(event){
    event.preventDefault();

To prevent opening the linked page.

Edit: Based on your update, I would think that is the problem, you might be submitting the form and without an action attribute, the form submits to itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜